Python正则表达式搜索以无法处理带有已编译模式的标志参数结束
我正在while循环中的正则表达式中使用Python正则表达式搜索以无法处理带有已编译模式的标志参数结束,python,regex,Python,Regex,我正在while循环中的正则表达式中使用search函数。 但是程序以ValueError:cannotprocessflags参数和编译模式结束。 如果我使用未编译的模式,它将以:ValueError:ASCII和UNICODE标志不兼容结束。我使用Python 3.81。如何解决这个问题 (我能够使用finditer成功运行程序 #!/usr/bin/python3 import re text = 'This island is beautiful and is large' pat
search
函数。
但是程序以ValueError:cannotprocessflags参数和编译模式结束
。
如果我使用未编译的模式,它将以:ValueError:ASCII和UNICODE标志不兼容
结束。我使用Python 3.81。如何解决这个问题
(我能够使用finditer
成功运行程序
#!/usr/bin/python3
import re
text = 'This island is beautiful and is large'
pattern = re.compile(r'\bis\b')
# pattern = r'\bis\b'
idx = 0
# match = re.search(pattern, text, pos=idx)
while True:
# while (match := re.search(pattern, text, idx)):
# pattern = re.compile(r'\bis\b')
match = re.search(pattern, text, idx)
if match == None:
break
print(match.group())
idx += match.endpos
re.search
的第三个参数定义了标志
如果要指定位置,请使用:
Pattern.search(string[, pos[, endpos]])
re.search只接受两个参数。def search(pattern,string,flags=0):。您试图用idx对这段代码做什么?@MarcusRenshaw重复应用于文本;也就是说,查找所有出现的内容。