Non greedy regex

T

tibetan.houdini

Can someone please explain why these expressions both produce the same
result? Surely this means that non-greedy regex does not work?

print re.sub( 'a.*b', '', 'ababc' )

gives: 'c'

Understandable. But

print re.sub( 'a.*?b', '', 'ababc' )

gives: 'c'

NOT, understandable. Surely the answer should be: 'abc'
 
C

Carsten Haese

Can someone please explain why these expressions both produce the same
result? Surely this means that non-greedy regex does not work?

print re.sub( 'a.*b', '', 'ababc' )

gives: 'c'

Understandable. But

print re.sub( 'a.*?b', '', 'ababc' )

gives: 'c'

NOT, understandable. Surely the answer should be: 'abc'

You didn't tell re.sub to only substitute the first occurrence of the
pattern. It non-greedily matches two occurrences of 'ab' and replaces
each of them with ''. Try replacing with some non-empty string instead
to observe the difference between the two behaviors.

-Carsten
 
T

Thomas Nelson

There's an optional count argument that will give what you want. Try
re.sub('a.*b','','ababc',count=1)
 
M

Maksim Kasimov

> print re.sub( 'a.*?b', '', 'ababc' )
>
> gives: 'c'


you've forgot "^" at the begging of the pattern (there is another 'ab' before 'c')


print re.sub( '^a.*?b', '', 'ababc' )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top