Python3 Regular Expression: of? of id and name yes patternno pattern Conditional Matching

  • 2021-12-05 06:28:00
  • OfStack

Directory 1. Purpose 2. Parameter Meaning 3. Syntax 4. Usage Explanation 4.1 Remove the question mark after (\ d +), that is to say, there must be group 1, not optional 4.2. Change it to match string 4.3. group is optional 4.4. Only match group 1, but yes does not match 5. Conclusion 1. By comparing 4.1 4.2 4.3, we can find that: 2. By 4.4, we can only match group Part, if yes-pattern And no-pattern If there is no match, only the contents matched by group will be output

1. Purpose

(?(id/name)yes-pattern|no-pattern) The role of is:

For the given id Or name Try to match first yes-pattern Part of the content;

If id Or name If the condition is not met, match it no-pattern Part of the content;

This sentence sounds very awkward, or it is still difficult to understand.

2. Meaning of parameters

Here, name or id refers to a content that has been grouped by group before the conditional match (at the current position)

name If it is a named grouping, that is named group Then the corresponding packet has a corresponding name, that is, the corresponding name;

id If it is an unnamed grouping, the unnamed group The corresponding packet also has the corresponding packet number, number called group, also called id, which corresponds to id;

yes-pattern If the previous group match is successful, the match between yes-pattern is executed here;

no-pattern If the previous group matching is unsuccessful, that is, the no-pattern is matched without finding the group content;

Note: The above yes-pattern And no-pattern Are ordinary regular expressions used to match what you want.

3. Grammar

If it exists no-pattern There should be a vertical bar in front of it to separate it yes-pattern And no-pattern

If you don't want to match no-pattern The part of, you can not write together with ''1.

Example:


>>> re.search(r'(\d+)?(?(1)\w+|jb51\.\w+)', 'jb51.com')>>> re.search(r'(\d+)?(?(1)\w+)', '100jb51')

Of which ?(1) That represents the serial number 1 group , which is above (\d+) Is not used in the second expression no-pattern

4. Detailed explanation of usage

Or the above example, let's transform 1 and explore their detailed usage and skills with examples

4.1 Remove the question mark after (\ d +), meaning that group 1 must exist, not optional

Execution:


>>> re.search(r'(\d+)(?(1)\w+|jb51\.\w+)', 'jb51.com')
>>>

The overall rule match failed, returning null, and the no-pattern section did not execute as expected.

4.2 Change 1 to match string


>>> re.search(r'(\d+)(?(1)\w+|jb51\.\w+)', '1jb51.com')

It can be matched. group1 and yes parts are matched

4.3 group is optional (add a question mark after group)


>>> re.search(r'(\d+)?(?(1)\w+|jb51\.\w+)', 'jb51')
>>>

If group does not match, and it is optional, then group will be considered unsuccessful and continue to perform no-pattern partial matching

4.4 Match only group 1 part, no yes part


>>> re.search(r'(\d+)(?(1)\w+|jb51\.\w+)', '1000')

Only the group 1 part is matched, but the yes part is not matched, and there is also a matching result, which shows that yes-pattern and no-pattern must be matched successfully

5. Conclusion

1. By comparing 4.1 4.2 4.3, we can find that:

When the group itself as a judgment condition in the special rule is not allowed to be empty, when the matching of the group to the target string is empty, the matching of the whole rule fails, so the following special rule as a part of the whole is naturally invalid. So if you want special rules, no-pattern Effective, it must be judged that the matching number of the corresponding group of conditions can be 0.

2. From 4.4, we can see that you can only match group Part, if yes-pattern And no-pattern If there is no match, only the contents matched by group will be output

For more tutorials on Python3 regular expressions, see the links below


Related articles: