Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 为什么re.VERBOSE会导致regex返回空?_Python_Regex - Fatal编程技术网

Python 为什么re.VERBOSE会导致regex返回空?

Python 为什么re.VERBOSE会导致regex返回空?,python,regex,Python,Regex,我有以下字符串: s = '<a class="biz-name"><span>Gus’s World Famous Fried Chicken</span></a>' s='Gus的世界著名炸鸡' 这将返回预期结果: regex = re.compile('''<a class="biz-name[\w\W]*?<span>(.*?)</span>''') regex.findall() ['Gus’s Worl

我有以下字符串:

s = '<a class="biz-name"><span>Gus’s World Famous Fried Chicken</span></a>'
s='Gus的世界著名炸鸡'
这将返回预期结果:

regex = re.compile('''<a class="biz-name[\w\W]*?<span>(.*?)</span>''')
regex.findall()
['Gus’s World Famous Fried Chicken']
regex=re.compile(''Read:

模式中的空白将被忽略,除非在字符类中,或前面有未转义的反斜杠,或在标记(如*?、(?:或(?p))中

问题是
re.VERBOSE
a类
与匹配的
aclass
相同,这不在您的输入中。您需要转义空格(并使用原始字符串以确保一般正确性):


re.compile(r')好吧,你读过这个标志的4行文档了吗?是的,但显然没有读过
regex = re.compile('''<a class="biz-name[\w\W]*?<span>(.*?)</span>''', re.VERBOSE)
regex.findall()
[]
re.compile(r'''<a\ class="biz-name[\w\W]*?<span>(.*?)</span>''', re.VERBOSE)
       raw ^     ^ escape space or it doesn't count in VERBOSE mode