Why do I need extra space in a regex pattern to make it work correctly?

When I write the following code:

m = re.findall('\sf.*?\s','a f fast and friendly dog');

I get the output :[' f ', ' friendly ']

But when I provide extra space between f and fast, I get the following output, which I expected from the previous one. The code is as follows

m = re.findall('\sf.*?\s','a f  fast and friendly dog');

Output:

[' f ', ' fast ', ' friendly ']

Can someone tell me why I am not getting a later output in the first case (without inserting extra space between f and fast)?

+4
source share
2 answers

\s. , ' f ' , 'fast' ' fast'. 'fast' , \s

+7

' f ' . 'fast and friendly dog'. fast , , .

, , .

+1

Source: https://habr.com/ru/post/1685214/


All Articles