(i) 0 Wide Assertion
Description: Examples of this article use Python to describe
First, what is the 0 wide assertion, the so-called 0-wide assertion is that it does not really match string literals, but only matches the corresponding position.
There are many such assertions in regular expressions, such as the starting position of the matching string or line ^ and/A, matching the string or the end of the line and/Z, the word boundary,/b, and so on. These simple do not say, today mainly on the definition of the use of the method (some information is called ring-type structure, name, not important). The definition is also a zero-width assertion, which is defined mainly in four categories:
1 , forward definition (positive sequence look around) (? =exp)
Indicates the right side to match the exp expression
" aaa111aaa, Bbb222&, 333CCC " = r "\d+ (? =[a-z]+)" = Re.compile (Strre) print reobj.findall (strtest)
Output: (regular means to find consecutive numbers and the last number to follow at least one A-Z character sequence)
['111'333']
2 , forward negative definition (negative order look around) (?!) EXP)
Indicates that the right mismatch does not match an exp expression
" aaa111aaa, Bbb222&, 333CCC " = R"\d+ (?! [a-z]+] = re.compile (strre )print reobj.findall ( Strtest)
Output: (regular means to find consecutive numbers, and the last digit cannot be followed by any sequence of characters within a-Z)
['one by one''222 '
3 , reverse definition (affirmative reverse look) (? <=exp) text must be fixed long
Indicates that the left side is to match an exp expression
" aaa111aaa, Bbb222&, 333CCC " = r "(? <=[a-z]) \d+" = Re.compile (Strre) print reobj.findall (strtest)
Output: (regular means to find consecutive numbers, and the first digit is preceded by a character in A-Z)
['111'222']
4 , reverse negation definition (negative reverse lookup) (? text must be fixed long
The left side does not match the EXP expression
" aaa111aaa, Bbb222&, 333CCC " = R"(? <! [A-z]) \d+ "reobj = re.compile (strre) print reobj.findall (strtest)
Output: (regular means finding consecutive numbers, and the first digit cannot be preceded by a character in A-Z)
['one', '333']
one of the issues to note here is that two backwards-defined expressions in Python and Perl exp allow only fixed-length text, such as the last fourth example, if the regular expression is written as: Strre = R "(?, the PY interpreter will error: Error:look-behind Requires fixed-width pattern.
(ii) named groups and no capturing groups
Python also has a named group and no capturing group
Named groups compare cattle x, which is also an extension of Python, that can use the same capturing group as a dictionary:
s ='tom:9527, sharry:0003'm= Re.match (r'(? p\w+):(? p\d+)', s)PrintM.group ()Printm.group (0)PrintM.group (1) PrintM.group (2) Printm.groups ()PrintM.group ("name") PrintM.group ("Num")
Output
tom:9527 Tom:9527 Tom 9527 ('Tom') '9527') Tom 9527
the use of no capturing group is similar, syntax: (?: exp) is just no capturing group matching exp, but does not capture matching text, nor assigns group numbers to this group .
python-regular 0 wide assertion and named Capture (class PHP)