I was trying to match all numbers in a string, this is an example code:
re.search(r'[0-9]*', '(123)').group() # outputs: '' re.search(r'[0-9]*', '123)').group() # outputs: '123' re.search(r'[0-9]+', '(123)').group() # outputs: '123'
Why is this strange behavior happening here?
Advertisement
Answer
In your case re.search
will stop on first match.
re.search(r'[0-9]*', '(123)').group()
– will search for digits starting from empty string to infinity long string, and since first char is (
, it marches empty string.
re.search(r'[0-9]*', '123)').group()
– since first char is digit, it will try to get longest match.
re.search(r'[0-9]+', '(123)').group()
– will search for digits starting from at least one char string to infinity long string, and since first char is (
, it will skip (
and will start from first digit and will try to get longest match.
*
is {0,}
and +
is {1,}
this is great resource to play with regex https://regex101.com/