Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的正则表达式错误:sre_常量。错误:无需重复_Python_Regex - Fatal编程技术网

python中的正则表达式错误:sre_常量。错误:无需重复

python中的正则表达式错误:sre_常量。错误:无需重复,python,regex,Python,Regex,只有在字母前后都有+时,我才试图返回True def SimpleSymbols(string): if re.search(r"(?<!+)\w(?!+)", string) is None : return True else: return False unescaped+是一个量词,它重复它修改的模式1次或多次。要匹配literal+,需要对其进行转义 然而,问题是什么?还有将做相反的事情:如果字符前面或后面有+,则匹配将失败 此外,\w不仅匹配字母,它还匹配字

只有在字母前后都有+时,我才试图返回True

def SimpleSymbols(string): 
if re.search(r"(?<!+)\w(?!+)", string) is None :
    return True
else:
    return False
unescaped+是一个量词,它重复它修改的模式1次或多次。要匹配literal+,需要对其进行转义

然而,问题是什么?还有将做相反的事情:如果字符前面或后面有+,则匹配将失败

此外,\w不仅匹配字母,它还匹配字母、数字、下划线和Python 3.x中的Unicode支持,或者Python 2.x中的re.U甚至更多字符。您可以改为使用[^\W\d]

使用

如果字符串中有+[字母]+,则返回True;如果不匹配,则返回False

见:

+是正则表达式中的特殊字符,您需要将其转义。必须将+转义,它是一个量词,但\w与字母不匹配。使用def SimpleSymbolsstring:return re.searchr\+[^\W\d\]\+,string
def SimpleSymbols(string): 
    return bool(re.search(r"\+[^\W\d_]\+", string))
import re

def SimpleSymbols(string): 
    return bool(re.search(r"\+[^\W\d_]\+", string))
print(SimpleSymbols('+d+dd')) # True
print(SimpleSymbols('ddd'))   # False