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