先読みアサーションを非キャプチャにする機能はありますか?のようなものbar(?:!foo)
とbar(?!:foo)
動作しない(Python)。
3195 次
2 に答える
3
「barber」で行うbar(?=ber)
と、「bar」は一致しますが、「ber」はキャプチャされません。
于 2012-03-29T10:15:12.843 に答える
1
あなたはアランの質問に答えませんでしたが、彼は正しいと思います。あなたは否定的な先読みの主張に興味があります。IOW-「bar」と一致しますが、「barfoo」とは一致しません。その場合、次のように正規表現を作成できます。
myregex = re.compile('bar(?!foo)')
for example, from the python console:
>>> import re
>>> myregex = re.compile('bar(?!foo)')
>>> m = myregex.search('barfoo')
>>> print m.group(0) <=== Error here because match failed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> m = myregex.search('bar')
>>> print m.group(0) <==== SUCCESS!
bar
于 2012-05-23T17:59:31.973 に答える