作者:cxsw01 | 来源:互联网 | 2022-12-08 16:51
我正在使用Python3。在我的应用程序中,用户可以直接输入正则表达式字符串,而应用程序将使用它来匹配某些字符串。例如,用户可以输入\t+
。但是,由于无法正确将其转换为正确的正则表达式,因此无法使其正常工作。我已经尝试过了,下面是我的代码。
>>> import re
>>> re.compile(re.escape("\t+")).findall(" ")
[]
但是,当我将regex字符串更改为时\t
,它将起作用。
>>> re.compile(re.escape("\t")).findall(" ")
['\t']
注意参数findall
是制表符。我不知道为什么它似乎不能正确显示在Stackoverflow中。
任何人都可以为我指出解决该问题的正确方向?谢谢。
1> Bruno Lubasc..:
编译用户输入
我假定用户输入是一个字符串,无论它来自您的系统是什么:
user_input = input("Input regex:") # check console, it is expecting your input
print("User typed: '{}'. Input type: {}.".format(user_input, type(user_input)))
这意味着您需要将其转换为正则表达式,这就是re.compile
它的作用。如果您使用re.compile
且未提供str
将要转换为正则表达式的有效值,则会抛出error。
因此,您可以创建一个函数来检查输入是否有效。您使用了re.escape
,因此我在功能中添加了一个标志以供使用re.escape
。
def is_valid_regex(regex_from_user: str, escape: bool) -> bool:
try:
if escape: re.compile(re.escape(regex_from_user))
else: re.compile(regex_from_user)
is_valid = True
except re.error:
is_valid = False
return is_valid
print("If you don't use re.escape, the input is valid: {}.".format(is_valid_regex(user_input, escape=False)))
print("If you do use re.escape, the input is valid: {}.".format(is_valid_regex(user_input, escape=True)))
如果您的用户输入是:\t+
,您将获得:
user_input = input("Input regex:") # check console, it is expecting your input
print("User typed: '{}'. Input type: {}.".format(user_input, type(user_input)))
但是,如果您的用户输入是:[\t+
,您将得到:
def is_valid_regex(regex_from_user: str, escape: bool) -> bool:
try:
if escape: re.compile(re.escape(regex_from_user))
else: re.compile(regex_from_user)
is_valid = True
except re.error:
is_valid = False
return is_valid
print("If you don't use re.escape, the input is valid: {}.".format(is_valid_regex(user_input, escape=False)))
print("If you do use re.escape, the input is valid: {}.".format(is_valid_regex(user_input, escape=True)))
请注意,它确实是无效的正则表达式,但是,通过使用re.escape
您的正则表达式将变为有效。这是因为re.escape
转义了所有特殊字符,将它们视为文字字符。因此,在情况下,你有\t+
,如果你使用re.escape
,你将寻找字符序列: \
,t
,+
而不是一个tab character
。
检查您的查询字符串
选择您要查看的字符串。例如,以下是一个字符串,其中引号之间的字符应为制表符:
string_to_look_in = 'This is a string with a " " tab character.'
您可以使用该repr
功能手动检查选项卡。
print(string_to_look_in)
print(repr(string_to_look_in))
>> If you don't use re.escape, the input is valid: True.
>> If you do use re.escape, the input is valid: True.
注意,通过使用制表符字符repr
的\t
表示,将显示该字符。
测试脚本
这是您尝试所有这些事情的脚本:
import re
string_to_look_in = 'This is a string with a " " tab character.'
print("String to look into:", string_to_look_in)
print("String to look into:", repr(string_to_look_in), "\n")
user_input = input("Input regex:") # check console, it is expecting your input
print("\nUser typed: '{}'. Input type: {}.".format(user_input, type(user_input)))
def is_valid_regex(regex_from_user: str, escape: bool) -> bool:
try:
if escape: re.compile(re.escape(regex_from_user))
else: re.compile(regex_from_user)
is_valid = True
except re.error:
is_valid = False
return is_valid
print("\nIf you don't use re.escape, the input is valid: {}.".format(is_valid_regex(user_input, escape=False)))
print("If you do use re.escape, the input is valid: {}.".format(is_valid_regex(user_input, escape=True)))
if is_valid_regex(user_input, escape=False):
regex = re.compile(user_input)
print("\nRegex compiled as '{}' with type {}.".format(repr(regex), type(regex)))
matches = regex. findall(string_to_look_in)
print('Mathces found:', matches)
else:
print('\nThe regex was not valid, so no matches.')