This article describes the functions and usage of regular surround viewing and backreferences in Java. Share it for your reference, as follows:
Around view
1. Surrounding the concept
Looking around, also known as zero width assertion, abbreviated assertion.
The surround view emphasizes the position (front or back), and the surround view expression must be matched to be successful.
Surround viewing can be considered as an additional judgment condition for virtual addition to its location and does not consume regular matching characters.
2. Looking around the basic expression
(?=Expression) The order is positive and the expression can be matched on the right side of the location.
(?!Expression) Sequence negation of the surrounding viewing means that the right side of the location cannot match the Expression
(?<=Expression) Reverse order positively looks around, indicating that the left side of the position can match the Expression
(?<!Expression) Reverse order negates the surrounding view, indicating that the left side of the position cannot match the Expression
Note: The right side of the order (=) matches, and the reverse order surround view has more < than the sequential surround view.
JavaScript only supports sequential surround viewing, but does not support reverse surround viewing.
Although both sequential surround view and reverse order surround view are supported in Java, reverse order surround view only supports expressions with a length determination. In reverse order surround view, quantifiers only support? and do not support other quantifiers with an uncertain length.
3. Use examples
3.1. Sequence affirmation and look around (?=Expression)
3.1.1. Match the file name without suffix with the end of the suffix ".txt"
【.+(?=/.txt)】
text:
txtfile.txt
exefile.exe
inifile.ini
Match result: txtfile
3.1.2. Match password (must contain letters (case insensitive), numbers, 6-16 digits)
【^(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9]{6,16}$】
(?=.*?[a-zA-Z]) defines at least one letter in the following characters, uses (?=.*?[0-9]) to define at least one number in the following characters, and finally defines the quantifier by actually matching the regular [a-zA-Z0-9]{6,16}.
3.2. Sequence negation of surround view (?!Expression)
3.2.1. Match tags other than <a></a>
【<(?!/?a/b)[^<]+?>】
Text: <a><a1></a>zxiaofan<div>com</d>iv>cc
Match result:
<a1>
<div>
</d>
3.2.2. Match the file name with the suffix that is not ".txt" at the end of the suffix
【.+(?!/.txt)】Expression errors because .+ has no specified position and is a greedy match. (So.+ can directly match txtfile.txt)
【(.+)(?!/.txt)/.[^.]+$】
text:
txtfile.txt
exefile.exe
inifile.ini
Match result:
exefile.exe
inifile.ini
3.3. Reverse order affirmation around (?<=Expression)
3.3.1. Match the content between the specified tags
【(?<=<div>)[^<]+(?=</div>)】
Text: <div>zxiaofan.com</div>
Matching result: zxiaofan.com
3.3.2. Get the value of the specified parameter
【(?<=name=).+】
text:
name=zxiaofan
age=20
level=6
Match result: zxiaofan
3.4. Reverse order negation of surround view (?<!Expression)
3.4.1. Get the value of non-specified parameters
【^[^=#]+=(?<!name=).+$】
text:
name=zxiaofan
age=20
level=6
#sex=1
Match result:
age=20
level=6
4. Comprehensive examples
4.1. Must contain letters, numbers, and special characters
【^(?=.*?[a-zA-Z])(?=.*?/d)(?![a-zA-Z/d]+$).+$】
Explanation: ^(?=.*?[a-zA-Z]) limit must have letters; (?=.*?/d) limit must have numbers; (?![a-zA-Z/d]+$) limit cannot be all numbers and letters.
4.2. Match the main domain name (match the top-level domain name)
【(?<=(?::///w{0,50}/.)?)(?:/w{0,50}/.)(?: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
Match result:
zxiaofan.com.cn
zxiaofan.com
zxiaofan.org.cn
Note: [?:] Do not capture matching text to automatically named groups, nor do they assign groups to this group. (It will not affect the result after removal)
Special domain name: Wanwang www.net.cn
4.3. Match 5 consecutive mobile phone numbers
【1[34578]/d{3}(/d)(?!/1{1})(/d)/2{4}】
text:
18328501111
183285111111
18328551111
183281111111
Match result:
183285111111
Note1:/1 matches the first set of contents
Note2: (?!/1{1}) filter 6 consecutive numbers
Backreferences
1. Backreference concept
Capture group: divide into several groups according to the () subexpression; each pair of () appears is a capture group; the engine will number the capture group, and the numbering rule is the left bracket (the order of occurrence from left to right, starting from 1.
Capture group naming:
(?<name>exp) Match exp and capture text into a group named name, which can also be written as (?'name'exp);
(?:exp) Match exp, does not capture the matching text to the automatically named group, nor does it assign a group number to this group.
Backreference:
/1 means the first group (abc); /2 means the second group;
/k<Word>: Refers to the group with the specified name.
2. Use examples
2.1. Match the file name with the same beginning and end
【([az]{3})[az]+/./1{1}】
text:
txtfile.txt
exefile.txt
fileini.ini
Match result:
txtfile.txt
Note: ([az]{3}) is the first group, and /1{1} means referring to the first group once (it cannot be written as /1{3} here).
PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools.VeVB.COM/regex/javascript
Regular expression online generation tool:
http://tools.VeVB.COM/regex/create_reg
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Regular Expression Skills", "Java Data Structure and Algorithm Tutorial", "Java Operation DOM Node Skills", "Java File and Directory Operation Skills Summary" and "Java Cache Operation Skills Summary"
I hope this article will be helpful to everyone's Java programming.