Skip to main content
 首页 » 编程设计

python之我的正则表达式适用于 regex101 但不适用于 python

2025年05月04日24haluo1

<分区>

所以我需要匹配被 | 包围的字符串。所以,模式应该只是 r"\|([^\|]*)\|",对吧?然而:

>>> pattern = r"\|([^\|]*)\|" 
>>> re.match(pattern, "|test|") 
<_sre.SRE_Match object at 0x10341dd50> 
>>> re.match(pattern, "  |test|") 
>>> re.match(pattern, "asdf|test|") 
>>> re.match(pattern, "asdf|test|1234") 
>>> re.match(pattern, "|test|1234") 
<_sre.SRE_Match object at 0x10341df30> 

它只匹配以|开头的字符串?它在 regex101 上工作得很好,如果重要的话,这是 python 2.7。我可能只是在这里做一些愚蠢的事情,所以我们将不胜感激。谢谢!