0

Why do the following two expressions return different results?

This returns - as expected:

re.sub(r'\w.*?-', '', 'test--')

But this returns the empty string ``, which is surprising to me.

re.sub(r'.*?-', '', 'test--')

I'm running Python 3.7.

Leo
  • 2,044
  • 19
  • 25
  • 1
    `re.sub` matches all occurrences and `.*?-` can also match `-`. `\w.*?-` can't match `-`. See https://regex101.com/r/DE0By6/2. Always use regex101 or similar sites to check your patterns. – Wiktor Stribiżew May 27 '19 at 09:17
  • Actually I did check. The problem turns out to be that `sub()` matches all occurrences when `count=0`, and the doc says "leftmost non-overlapping occurrences", which I found confusing, especially given that there is also `subn` method. – Leo May 27 '19 at 10:04
  • True, that sounds confusing. I think the point of using "leftmost" is to stress the fact that the matches are searched for from left to right. Still, "occurrence**s**" means *all* occurrences are found by default. – Wiktor Stribiżew May 27 '19 at 10:07

0 Answers0