gpt4 book ai didi

python regex [.]+ 应该代表任何单个或重复的字符

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:38 24 4
gpt4 key购买 nike

x='andi'
print re.search('an[.]+',x)

它返回。它是否应该匹配字符串。在文档中,.(点)可以代表任何字符。而 + 表示 1 次或多次重复。所以对于 [.]+,它应该匹配字符串中的 d

谁能解释一下?为什么它返回 None

最佳答案

当您使用时:

print re.search('an[.]+', x)

它匹配文字点,因为点在 [ ] 中,即特殊正则表达式元字符失去其特殊含义的字符类。

你应该使用:

print re.search('an.+', x)

示例:

>>> print re.search('an.+', x).group()
andi

关于python regex [.]+ 应该代表任何单个或重复的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35732614/

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