Java regular loop view and reverse reference functions and Usage Details, java loop view Usage Details

Source: Internet
Author: User

Java regular loop view and reverse reference functions and Usage Details, java loop view Usage Details

This example describes the functions and usage of Java regular loop view and reverse reference. We will share this with you for your reference. The details are as follows:

View

1. View Concept

Loop view, also known as zero-width assertions, is short for assertions.

The highlighted position (front or back) of the surround view must match the surround View Expression to make the match successful.

The loop view can be considered as an additional judgment condition that is added to its location, without consuming regular matching characters.

2. Basic loop View Expression

(? = Expression) the order is certainly the loop, indicating that the right side of the location can match the Expression
(?! Expression) the order is negative, indicating that the right side of the location cannot match the Expression.
(? <= Expression) the reverse direction is certainly the loop view, indicating that the left side of the location can match the Expression.
(? <! Expression) in reverse order, which indicates that the left side of the location cannot match the Expression.

Note:Match on the right side of the sequence (=). There are more reverse-order view than sequential view <.

In JavaScript, only sequential surround views are supported, and reverse surround views are not supported.

In Java, although both sequential and reverse view are supported, reverse view only supports expressions with a fixed length. In reverse view, only quantifiers are supported ?, Other quantifiers with an indefinite length are not supported.

3. Example

3.1. The sequence must be (? = Expression)

3.1.1when matching the suffix is the name of the file without a suffix. txt.

[. + (? = \. Txt )]

Text:

Txtfile.txt
Exefile.exe
Inifile. ini

Matching result: txtfile

3.1.2 matched password (must contain letters (case-insensitive), numbers, 6-16 characters)
[^ (? = .*? [A-zA-Z]) (? = .*? [0-9]) [a-zA-Z0-9] {6, 16} $]

(? = .*? [A-zA-Z]) specifies at least one letter after a character (? = .*? [0-9]) limit at least one number in the subsequent characters, and finally limit the quantifiers by actually matching the regular [a-zA-Z0-9] {6, 16.

3.2. Sequential negative view (?! Expression)

3.2.1 match tags other than <a> </a>

[<(?! /? A \ B) [^ <] +?>]

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

Matching result:

<A1>
<Div>
</D>

3.2.2suffix is not the suffix-specific file name of ". txt ".

[. + (?! \. Txt)] The expression is incorrect because. + does not have a specified position and is greedy. (Because of this, TXT file.txt can be directly matched)
[(. + )(?! \. Txt) \. [^.] + $]

Text:

Txtfile.txt
Exefile.exe
Inifile. ini

Matching result:

Exefile.exe
Inifile. ini

3.3. forward view (? <= Expression)

3.3.1 Matching content between specified tags

【(? <= <Div>) [^ <] + (? = </Div> )]

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

Matching result: zxiaofan.com

3.3.2 obtain the value of a specified parameter

【(? <= Name =). +]

Text:

Name = zxiaofan
Age = 20
Level = 6

Matching result: zxiaofan

3.4. Negative backward view (? <! Expression)

3.4.1 obtain the value of an unspecified Parameter

[^ [^ = #] + = (? <! Name =). + $]

Text:

Name = zxiaofan
Age = 20
Level = 6
# Sex = 1

Matching result:

Age = 20
Level = 6

4. Example

4.1. It must contain letters, numbers, and special characters

[^ (? = .*? [A-zA-Z]) (? = .*? \ D )(?! [A-zA-Z \ d] + $). + $]

Explanation: ^ (? = .*? [A-zA-Z]) the limit must contain letters ;(? = .*? \ D) the limit must contain numbers ;(?! [A-zA-Z \ d] + $) cannot contain all numbers and letters.

4.2 match primary domain name (match 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

Matching result:

Zxiaofan.com.cn
Zxiaofan.com
Zxiaofan.org.cn

Note :【? :] Does not capture the matched text to the automatically named group, nor does it configure the group. (Results are not affected after removal)
Special Domain Name: www.net.cn

4.3. Match the phone number of the fifth serial number

[1 [34578] \ d {3} (\ d )(?! \ 1 {1}) (\ d) \ 2 {4 }]

Text:

18328501111
18328511111
18328551111
18328111111

Matching result:

18328511111

Note1: \ 1 match the first group of content
Note2 :(?! \ 1 {1}) filter the serial number of 6

Reverse reference

1. Reverse reference concept

Capture group: the Group is divided into several groups according to the () subexpression. Each pair () is a capture group. The engine numbers the capture group, the numbering rules are left parentheses (from left to right, starting from 1.

Capture group name:

(? <Name> exp) matches exp and captures the text to a group named name. It can also be written (? 'Name' exp );
(? : Exp) matches the exp, does not capture the matched text to the automatically named group, and does not assign the group number to this group.

Reverse reference:

\ 1 indicates the first group (abc); \ 2 indicates the second group;
\ K <Word>: refers to the group with the specified name.

2. Example

2.1. Match the same name at the beginning and end

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

Text:

Txtfile.txt
Exefile.txt
Fileini. ini

Matching result:

Txtfile.txt

Note :( [a-z] {3}) is the first group. \ 1 {1} indicates referencing the first group once (\ 1 {3} cannot be written here }).

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.