gpt4 book ai didi

python - Python 中的正则表达式 - 使用组

转载 作者:太空狗 更新时间:2023-10-30 00:07:49 25 4
gpt4 key购买 nike

我是正则表达式的新手,为什么不输出“present”?

tale = "It was the best of times, ... far like the present ... of comparison only. "
a = re.compile('p(resent)')
print a.findall(tale)

>>>>['resent']

最佳答案

如果您想在此处匹配确切的单词 present,请尝试这样的操作:

In [297]: tale="resent present ppresent presentt"

In [298]: re.findall(r"\bpresent\b",tale)
Out[298]: ['present']

关于python - Python 中的正则表达式 - 使用组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14027286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com