() indicates the capture group, () will save the matching values in each group from left to right, marked by the left bracket of the group, the group number of the first appearing group is 1, the second is 2, and so on
(?:) indicates a non capture group. The only difference between a capture group and a non capture group is that,The values matched by non capture groups will not be saved
Code language:javascript
copy
import rea = "123abc456ww"pattern = "([0-9]*)([a-z]*)([0-9]*)"print(re.search(pattern,a).group(0,1,2,3))pattern = "(?:[0-9]*)([a-z]*)([0-9]*)"print(re.search(pattern,a).group(0,1,2))
You can see that the first [0-9] * matched by (?: [0-9] *) has not been saved, that is, the matched "123" has not been saved, while ([0-9] *) has been saved.
Group (0) in python returns the matched whole
(?: pattern) It is useful to use the "or" character (|) to combine various parts of a pattern.For example, 'industry (?: y | ies)' is a simpler expression than 'industry | industries'.Because it's meaningless to store "y" or "ies" separately
Code language:javascript
copy
a = "British industry"pattern = "industr(?:y|ies)"print(re.search(pattern,a).group(0))#Group (1) will report an error because the captured "y" is not savedpattern = "industr(y|ies)"print(re.search(pattern,a).group(0, 1))
(?=pattern)
Look ahead positive assert matches the position in front of pattern.This is a non acquisition match, that is, the match does not need to be acquired for future use.
To put it simply, take xxx (?=pattern) as an example to captureContent ending in pattern
For example, "Windows (?=95 | 98 | NT | 2000)" can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3.1".The pre query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than after the character containing the pre query.
(?!pattern)
Negative assert matches the search string at the beginning of any string that does not match the pattern.This is a non acquisition match, that is, the match does not need to be acquired for future use.
In short, take xxx (?! pattern) as an example, that is, captureContent not ending in pattern
For example, "Windows (?! 95 | 98 | NT | 2000)" can match "Windows" in "Windows3.1", but cannot match "Windows" in "Windows2000".The pre query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than after the character containing the pre query.
(?<=pattern)
The look behind positive pre check is similar to the positive positive pre check, but in the opposite direction.
In brief, take (?<=pattern) xxx as an example, that is, captureStart of patternThe content of xxx.
For example“(?<=95|98|NT|2000)Windows"Can match"2000WindowsInWindows", but cannot match"3.1WindowsInWindows"。