Java regular circumferential and backreference features and usage in detail

  • 2020-12-20 03:33:36
  • OfStack

This article gives an example of Java's canonical circumnavigation and backreference functions and uses. To share for your reference, the details are as follows:

Look around the

1. Concept of visibility

Look around, also known as zero width assertion, short for assertion.

The loopback emphasis position (front or back) must match the loopback expression in order to match successfully.

A circle can be thought of as an additional condition to be added to its location virtually, and does not consume regular matching characters.

2. Look around the basic expression

(& # 63; =Expression) The sequence must be looked around, indicating that the right side of the location can match Expression
(& # 63; ! Expression) sequential negative loop, indicating that Expression cannot be matched to the right of the position
(? < =Expression) Must be viewed in reverse order, indicating that the position to the left can match Expression
(? < ! Expression) reverse negative scan, indicating that the left side of the location cannot match Expression

Note: Sequence (=) matches on the right, with more reverse loops than sequential loops < .

Only sequential circling is supported in JavaScript, not reverse circling.

In Java, although both sequential and inverting loops are supported, inverting loops only support expressions with defined length, while quantifiers in inverting loops only support ? , other quantifiers of variable length are not supported.

3. Use examples

3.1. Look around in definite order (? =Expression)

3.1.1. Match the filename without suffix ending with ".txt"

【. + (= \. & # 63; txt).

Text:

txtfile.txt
exefile.exe
inifile.ini

Match result: txtfile

3.1.2 Matching password (must contain letters (case-insensitive), numbers, 6-16 bits)
【 ^ (& # 63; =. * & # 63; [a zA - Z]) (= & # 63; m. * & # 63; [0-9]) [a - zA - Z0-9] 6 16th} {$]

(& # 63; =. * & # 63; [ES75en-ES76en-ES77en]) qualifies at least one letter of the following character, using (? =. * & # 63; [0-9]) qualifies at least 1 number of characters, and qualifies quantifiers by actual matching [a-zA-Z0-9]{6,16}.

3.2 Sequential negative loop (? ! Expression)

3.2.1 Match division < a > < /a > Outside label

【 < (?!/?a\b)[^ < ]+? > 】

Text: < a > < a1 > < /a > zxiaofan < div > com < /d > iv > cc

Matching results:

< a1 >
< div >
< /d >

3.2.2. Filename with a suffix other than ".txt "at the end of matching suffix

[.+(?!\.txt)] expression is incorrect because.+ has no specified position and is a greedy match. (So.+ matches txtfile. txt directly)
【 (. +) (& # 63;! \. txt) \. [^.] + $]

Text:

txtfile.txt
exefile.exe
inifile.ini

Matching results:

exefile.exe
inifile.ini

3.3. Check the reverse sequence (? < =Expression)

3.3.1. Match the contents between specified labels

【 (& # 63; < = < div > )[^ < ]+(?= < /div > ).

Text: < div > zxiaofan.com < /div >

Match result: ES192en. com

3.3.2. Get the value of the specified parameter

【 (& # 63; < +] = name =).

Text:

name=zxiaofan
age=20
level=6

Match result: zxiaofan

3.4 Reverse negative circling (? < !Expression)

3.4.1. Get the value of the non-specified parameter

【 ^ [^ = #] + = (& # 63; < ! + $] name =).

Text:

name=zxiaofan
age=20
level=6
#sex=1

Matching results:

age=20
level=6

4. Comprehensive examples

4.1. It must contain letters, numbers and special characters

【 ^ (& # 63; =. * & # 63; [a zA - Z]) (= & # 63; m. * & # 63; \ d) (the & # 63;! [a zA - Z \ d] + $). + $.

Explanation: ^ (& # 63; =. * & # 63; [ES256en-ES257en-ES258en]) restrictions must have letters; (& # 63; =. * & # 63; \d) Limit must have a number; (& # 63; ! [ES260en-ES261en-ES262en \d]+$) restrictions cannot be all numbers and letters.

4.2 Match primary domain name (match top-level domain name)

【 (& # 63; < = (& # 63; : : / / \ w {0, 50} \.) the & # 63;) (& # 63; : \ w {0, 50} \.) (the & # 63; : com \. cn | net \. cn | org \. cn | com | net | org | cn | biz | info | cc | tv).

Text:

vip.zxiaofan.com.cn
http://zxiaofan.com/123
www.zxiaofan.org.cn

Matching results:

zxiaofan.com.cn
zxiaofan.com
zxiaofan.org.cn

Note: [?:] does not capture matching text to automatically named groups and does not assign groups to this group well. (The results will not be affected after removal)
Special domain name: 10,000 www. net. cn

4.3 Match the mobile phone number with 5 serial numbers

【 1 [34578] \ d {3} (\ d) (the & # 63;! \ {1}) (\ d) 2 {4} \]

Text:

18328501111
18328511111
18328551111
18328111111

Matching results:

18328511111

Note1: \1 matches the contents of group 1
Note2: (& # 63; ! \1{1}) Filter 6 serial numbers

backreferences

1. Backreference concepts

Capture group: Divided into several groups according to () subexpression; Every time a pair () appears, there is a capture group; The capture group is numbered by the engine, with the left parenula (the order in which it appears from left to right, starting at 1).

Capture group name:

(? < name > exp) matches exp and captures the text into a group named name, which can also be written as (? 'name exp);
(& # 63; :exp) matches exp, does not capture matching text to automatically named groups, and does not assign group numbers to this group.

Back reference:

\1 for group 1 (abc); \2 represents group 2;
\k < Word > : Refers to the group with the specified name.

2. Use examples

2.1 match filenames with the same beginning and end

[[[a - z] {3}) [a z] + \. \ {1} 1 】

Text:

txtfile.txt
exefile.txt
fileini.ini

Matching results:

txtfile.txt

Note :([a-z]{3}) is group 1, \1{1} refers to group 1 once (\1{3} cannot be written here).

PS: Here are two more handy regular expression tools for you to use:

JavaScript Regular Expression online test tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

For more information about java algorithm, please visit Java Regular Expression Techniques, Java Data Structure and Algorithm Tutorial, Java Node Techniques summary, Java File and Directory Techniques Summary and Java Cache Techniques Summary.

I hope this article has been helpful in java programming.


Related articles: