How does Python Re Module work in this example?

What is the process of matching this regex? I do not understand why the explicit group is "c". This is part of the code taken from the Python Re Module Doc.

>>> m = re.match("([abc])+", "abc") >>> m.group() 'abc' >>> m.groups() ('c',) 

Also, what about:

 >>> m = re.match("([abc]+)", "abc") >>> m.group() 'abc' >>> m.groups() ('abc',) 

and

 >>> m = re.match("([abc])", "abc") >>> m.group() 'a' >>> m.groups() ('a',) 

Thanks.

+4
source share
2 answers
 re.match("([abc])+", "abc") 

Corresponds to a group consisting of a, b or c. The group at the end of this is the last character found in the character class, since the match is greedy, ends with the last matching character, which is c .

 m = re.match("([abc]+)", "abc") 

Corresponds to a group that contains one or more consecutive occurrences of a, b, or c. The corresponding group at the end is the largest transcendental group a, b or c.

 re.match("([abc])", "abc") 

Matches either a, b, or c. A match group will always be the first matching character at the beginning of a line.

+6
source

In the first example ([abc])+ creates a group for each character a, b or c found. c is an explicit group because it is the last character that matches the regular expression:

 >>> re.match("([abc])+", "abca").groups() ('a',) 

In your second example, you create one group that matches one or more characters a, b, or c in a string. Thus, you create one group for abc . If we continue abc , the group will continue with the line:

 >>> re.match("([abc]+)", "abca").groups() ('abca',) 

In the third example, the regular expression searches for exactly one character, which is either a, a, or c. Since a is the first character in abc , you get a. This changes if we change the first character in the string:

 >>> re.match("([abc])", "cba").group() 'c' 
+3
source

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


All Articles