Python3 regular expression: (?(id/name)yes-pattern|no-

Python3 regular expression : (?(id/name)yes-pattern|no-pattern)conditional matching

1. Purpose

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

For the given idor name, first try to match  yes-patternpart of the content;

If the idor nameconditions are not met, go to match no-patternpart of the content;

This sentence still sounds awkward, or it's hard to understand all of a sudden.

2. Parameter meaning

The name or id here is for a content that has been grouped by group before the conditional match (current position)

name: If it is a named group, that is named group, the corresponding group has a corresponding name, that is , the corresponding name is referred to here;

id: If it is an unnamed group, that is unnamed group, the corresponding group also has a corresponding group number, which is called the number of the group, also called id, which corresponds to the id here;

yes-pattern: If the previous group matches successfully, then the yes-pattern matching will be performed here;

no-pattern: If the previous group match is unsuccessful, that is, no group content that matches the group content is found, then no-pattern will be matched;

NOTE: The above yes-patternand no-patternare common regular expressions to match the desired content.

3. Grammar

If it exists no-pattern, there must be a vertical bar'|' in front to separate yes-patternandno-pattern

If you don't want to match no-patternthe part, you can leave it out with the'|'.

Examples:

1

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

 

The ?(1)representative number is 1 group, which is the above (\d+), and it is not used in the second expressionno-pattern

4. Detailed usage

Still the above example, let’s change it and use examples to explore their detailed usage and skills

 

4.1 Remove the question mark after (\d+), which means that group 1 must exist, not dispensable

carried out:

1

2

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

>>>

The overall rule fails to match, the return is empty, and the no-pattern part is not executed as expected.

 

4.2 Change the matching string

1

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

It can be matched, and both the group1 and yes parts are matched

 

4.3 The dispensation of group (add question mark after group)

1

2

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

>>>

If the group is not matched, and it is dispensable, then the group will be considered unsuccessful and continue to perform no-pattern partial matching

 

4.4 Only match group 1 part, yes part does not match

1

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

Only match the group 1 part, the yes part is not matched, and there is a matching result, which means that yes-pattern and no-pattern may not necessarily match successfully

5 Conclusion

1. Through the comparison of 4.1 4.2 4.3, it can be found that:

When the group as the judgment condition in the special rule is not allowed to be empty, if the group matches the target string to be empty, the overall rule fails to match, so the following special rules as a part of the whole will naturally be It's not valid anymore. Therefore, in order for the special rule to no-patterntake effect, it must be judged that the matching number of the group corresponding to the condition can be 0.

2. It can be seen from 4.4 that only the grouppart can be matched , if the yes-patternand no-patterndoes not match, only the content matched by the group will be output

Guess you like

Origin blog.csdn.net/zl17822307869/article/details/113840409