When I write following code:
m = re.findall('\sf.*?\s','a f fast and friendly dog');
I get output: [' f ', ' friendly ']
But when I provide extra space between f & fast, I get following output which I expected from the previous one. Code is as follows
m = re.findall('\sf.*?\s','a f fast and friendly dog');
Output:
[' f ', ' fast ', ' friendly ']
Can anyone tell me why I am not getting later output in first case (without inserting extra space between f & fast)?
Because your pattern ends in \s
. Regex matches are non-overlapping, so the first match ' f '
matches the trailing space, making the rest of the string begin with 'fast'
instead of ' fast'
. 'fast'
does not match a pattern starting with \s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With