home page
study
activity
Special area
Sphere
tool
release
Community homepage > special column > [Regular] The? : ?! ? = ? <=

[Regular] The? : ?! ? = ? <=

 Author's picture
Unique Chat
release to 2021-06-10 15:59:21
release to 2021-06-10 15:59:21
5.7K zero zero
Code runnable
report
Total running times: zero
Code runnable

(?:pattern)

() 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 value of non capture group matching will not be saved

Code language: javascript
code Number of runs: zero
function
copy
 import re a = "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
code Number of runs: zero
function
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 saved pattern = "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 capture Content 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, capture Content 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, capture Start of pattern The content of xxx.

For example“ (?<=95|98|NT|2000)Windows "Can match" 2000Windows In Windows ", but cannot match" 3.1Windows In Windows "。

Participation in this article Tencent Cloud We Media Synchronous Exposure Plan , share from the author's personal site/blog.
Original publication: 2021-06-02, In case of infringement, please contact cloudcommunity@tencent.com delete

This article is shared from Author's personal site/blog   Go to view

In case of infringement, please contact cloudcommunity@tencent.com Delete.

Participation in this article Tencent Cloud We Media Synchronous Exposure Plan Welcome you who love writing!

comment
Sign in Post participation comments
zero Comments
degree of heat
newest
Recommended reading
catalog
  • (?:pattern)
  • (?=pattern)
  • (?!pattern)
  • (?<=pattern)
Coupon collection
Question Archive Column Quick news article archiving Keyword archiving Developer manual archiving Developer's Manual Section Archiving