Is there an easy way to verify that the given character has a special regex function?
有没有一种简单的方法可以验证给定字符是否具有特殊的正则表达式函数?
Of course I can collect regex characters in a list like ['.', "[", "]", etc.]
to check that, but I guess there is a more elegant way.
当然我可以在['。',“[”,“]”等列表中收集正则表达式字符来检查,但我想有更优雅的方式。
4 个解决方案
#1
5
You could use re.escape
. For example:
你可以使用re.escape。例如:
>>> re.escape("a") == "a"
True
>>> re.escape("[") == "["
False
The idea is that if a character is a special one, then re.escape
returns the character with a backslash in front of it. Otherwise, it returns the character itself.
这个想法是,如果一个角色是一个特殊角色,那么re.escape会返回前面带有反斜杠的角色。否则,它返回角色本身。
#2
0
You can use re.escape
within all
function as following :
您可以在所有函数中使用re.escape,如下所示:
>>> def checker(st):
... return all(re.escape(i)==i for i in st)
...
>>> checker('aab]')
False
>>> checker('aab')
True
>>> checker('aa.b3')
False
#3
-1
Per the documentation, re.escape
will (emphasis mine):
根据文档,re.escape将(强调我的):
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
返回字符串,所有非字母数字反向;如果要匹配可能包含正则表达式元字符的任意文字字符串,这非常有用。
So it tells you whether a character could be a meaningful one, not whether it is. For example:
所以它告诉你一个角色是否有意义,而不是它是否。例如:
>>> re.escape('&') == '&'
False
This is useful for processing arbitrary strings, as it ensures that all control characters are escaped, but not for telling you which actually needed to be. The simplest approach, in my view, is the one dismissed in the question:
这对于处理任意字符串很有用,因为它可以确保所有控制字符都被转义,但不能告诉您实际需要的字符串。在我看来,最简单的方法是在问题中被驳回的方法:
char in set(r'.^$*+?{}[]\| ')
#4
-1
Elegance lies in the eyes of the beholder, however (IMHO) this (below) is the most generic/"timeproof" way of checking if a character is considered to be special by the Python Regex engine -
优雅在于旁观者的眼睛,然而(恕我直言)这(下图)是检查Python Regex引擎是否认为某个角色特殊的最通用/“时间”方式 -
def isFalsePositive(char):
m = re.match(char, 'a')
if m is not None and m.end() == 1:
return True
else:
return False
def isSpecial(char):
try:
m = re.match(char, char)
except:
return True
if m is not None and m.end() == 1:
if isFalsePositive(char):
return True
else:
return False
else:
return True
P.S. -
isFalsePositive() may be overkill to check the special case of '.' (dot). :-)
附: - isFalsePositive()可能有点过分来检查'。'的特殊情况。 (点)。 :-)
#1
5
You could use re.escape
. For example:
你可以使用re.escape。例如:
>>> re.escape("a") == "a"
True
>>> re.escape("[") == "["
False
The idea is that if a character is a special one, then re.escape
returns the character with a backslash in front of it. Otherwise, it returns the character itself.
这个想法是,如果一个角色是一个特殊角色,那么re.escape会返回前面带有反斜杠的角色。否则,它返回角色本身。
#2
0
You can use re.escape
within all
function as following :
您可以在所有函数中使用re.escape,如下所示:
>>> def checker(st):
... return all(re.escape(i)==i for i in st)
...
>>> checker('aab]')
False
>>> checker('aab')
True
>>> checker('aa.b3')
False
#3
-1
Per the documentation, re.escape
will (emphasis mine):
根据文档,re.escape将(强调我的):
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
返回字符串,所有非字母数字反向;如果要匹配可能包含正则表达式元字符的任意文字字符串,这非常有用。
So it tells you whether a character could be a meaningful one, not whether it is. For example:
所以它告诉你一个角色是否有意义,而不是它是否。例如:
>>> re.escape('&') == '&'
False
This is useful for processing arbitrary strings, as it ensures that all control characters are escaped, but not for telling you which actually needed to be. The simplest approach, in my view, is the one dismissed in the question:
这对于处理任意字符串很有用,因为它可以确保所有控制字符都被转义,但不能告诉您实际需要的字符串。在我看来,最简单的方法是在问题中被驳回的方法:
char in set(r'.^$*+?{}[]\| ')
#4
-1
Elegance lies in the eyes of the beholder, however (IMHO) this (below) is the most generic/"timeproof" way of checking if a character is considered to be special by the Python Regex engine -
优雅在于旁观者的眼睛,然而(恕我直言)这(下图)是检查Python Regex引擎是否认为某个角色特殊的最通用/“时间”方式 -
def isFalsePositive(char):
m = re.match(char, 'a')
if m is not None and m.end() == 1:
return True
else:
return False
def isSpecial(char):
try:
m = re.match(char, char)
except:
return True
if m is not None and m.end() == 1:
if isFalsePositive(char):
return True
else:
return False
else:
return True
P.S. -
isFalsePositive() may be overkill to check the special case of '.' (dot). :-)
附: - isFalsePositive()可能有点过分来检查'。'的特殊情况。 (点)。 :-)