Why does re.search (r '(ab *)', 'aaAaABBbbb', re.I) in python give the result 'a' instead of 'ABBbbb', although 're.I' is used?

In python, re.search() checks for compliance anywhere in the string (this is what Perl does by default).

So, why don't we get the output of "ABBbbb" in Ex (1) , as we found in Ex (2) and Example (3) below.

Ex (1)

 >>> s=re.search(r'(ab*)','aaAaABBbbb',re.I) >>> print s.group() a 

Ex (2)

 >>> s=re.search(r'(ab.*)','aaAaABBbbb',re.I) >>> print s.group() ABBbbb 

Ex (3)

 >>> s=re.search(r'(ab+)','aaAaABBbbb',re.I) >>> print s.group() ABBbbb 
+5
source share
2 answers

Example 1 searches for a followed by zero or more b , ignoring case. This matches the beginning of the line. The regex engine will see this match and use it. He will not look for any other matches.

Example 2 searches for ab followed by as many lines as it can. Example 3 searches for a , following at least one b . The difference is that each of them needs at least one b , while in example 1 there isn’t.

+6
source

search :

checks compliance anywhere in the string (this is what Perl does by default).


 re.search(r'(ab*)', 'aaAaABBbbb', re.I) 

This will try to match a (ignored case) followed by zero or more b . He will find a match in the first a , since he is followed by zero b , and he returned it.

 re.search(r'(ab.*)', 'aaAaABBbbb', re.I) 

This will try to match a , then b , and then with anything ( .* Is greedy). It matches ABBbbb because it is the first sequence that matches the regular expression.

 re.search(r'(ab+)', 'aaAaABBbbb', re.I) 

Finally, this will correspond to a followed by at least one b (again, the case is ignored). This is the first ABBbbb match and it is returning.

+2
source

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


All Articles