2012-03-25 8 views
4

lookahead 어썰트를 캡쳐하지 않는 기능이 있습니까? bar(?:!foo)bar(?!:foo) 같은 것은 작동하지 않습니다 (Python).정규식 비 캡쳐 lookahead 어설 션

+5

Lookaheads이 * 비 캡처 *입니다. 당신은 아마도 * 부정적 * 선견자를 찾고 있습니까? 그건 그냥'(?! foo)'입니다. [ref] (http://www.regular-expressions.info/lookaround.html) –

답변

2

"barber"에 bar(?=ber)를 입력하면 "bar"는 일치하지만 "ber"는 캡처되지 않습니다.

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