Daniel Wu
Daniel Wu

Reputation: 6003

re.sub: non-greedy doesn't work as expected

I have the following code in ipython. I expect it to remove the beginning "ab" since .*? is a non-greedy one. But why it remove all the way up to the last b?

  In [15]: b="abcabcabc"

  In [16]: re.sub(".*?b","",b)
  Out[16]: 'c'

Upvotes: 1

Views: 370

Answers (2)

Ohad Eytan
Ohad Eytan

Reputation: 8464

The python docs says:

The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced

So, you can call re.sub with count=1 to get your desired result:

re.sub(".*?b", "", b, 1)
#output
'cabcabc'

Upvotes: 1

Sundeep
Sundeep

Reputation: 23667

That is because, by default, re.sub() will search and replace all occurrences

>>> import re
>>> b="abcabcabc"
>>> re.sub(".*?b","",b)
'c'
>>> re.sub("^.*?b","",b)
'cabcabc'
>>> re.sub(".*?b","",b, count=1)
'cabcabc'
>>> re.sub(".*?b","",b, count=2)
'cabc'


From doc

re.sub(pattern, repl, string, count=0, flags=0)

Upvotes: 3

Related Questions