Code Analysis of a perl extension Regular Expression

Source: Internet
Author: User

Copy codeThe Code is as follows: my $ ip = "192.168.0.1 | 192.168.0.2 | 192.168.0.1 ";
If ($ ip = ~ /
^
(? :
((? : \ D {1, 3} \.) {3} \ d {1, 3 })
(? =
(? :
\ | (?! \ 1 )(? 1)
)*
\ Z
)
\ |
)*
(? 1)
$
/X ){
Print "match \ n ";
}

According to the instructions in the perlre document, 1.1 points are explained. The first is/x, which is used to remove spaces in regex. Otherwise, it would be too ugly to write them in a line. Then it is ^, which indicates starting from the beginning; then (? :, Which indicates that the brackets are not recorded in the reverse reference $ &; then ((? : \ D {1, 3}.) {3} \ d {1, 3 (? :, That is to say, this line matches an ip address and calculates it as $1. Then (? =, This indicates that the regular expression of the above line of ip must be followed by the regular expression that complies with the definition in this brackets, it is also not included in $ & (the term is "0-width positive forward assertions", right ?); Then a separate ip address |; then (?!, This indicates that the content in the brackets cannot appear, and it is not included in $ & (the term is "0-width negation of forward assertions", right ?); Then it is \ 1. This is the previously captured $1, which is combined with the assertion interpreted by the uplink. That is, it cannot be followed by an ip address that matches the previous one. Then (? 1. This indicates that the regular expression $1 is captured before, that is, the new ip address is captured without repeating the ip address; then yes). This) is closed to | (? :, That is, | the ip address can be repeated multiple times, and then \ z. This is the string boundary, which is equivalent to $ in a single line. In this example, it can be exchanged and used here, is to make (?! \ 1) check until the end; then), close (? =; Then yes | and). Here it is closed to ^ (, indicating that the ip address meets the non-repeated ip condition | the format is constantly regular match; then (? 1) $, defines the last ip address, and uses the same regular expression as $1, that is, the string must have at least one ip address. OK. The explanation is complete. In fact, from the back-to-back view, it is clearer ~~ In addition, perlre is concentrated in (?? {CODE}) contains the following statement: "In perl 5.12.x and earlier, because the regex engine was not re-entrant, delayed code cocould not safely invoke the regex engine either directly with "m //" or "s //"), or indirectly with functions such as "split ". ", and (? R) and (?? {CODE}) is a similar and simple task, so if the perl version in the linux release is not high enough, it cannot be used here (? 1) You need to write it again. You can judge this way:Copy codeThe Code is as follows: my $ re = $ ^ V lt v5.14? '(? : \ D {1, 3 }\.?) {4 }':'(? 1 )';
My $ ip = "192.168.0.1 | 192.168.0.2 | 192.168.0.3 | 192.168.0.4 | 192.168.0.5 ";
If ($ ip = ~ M/
^
(? :
((? : \ D {1, 3 }\.?) {4 })
(? =
(? :
\ | (?! \ 1) $ re
)*
\ Z
)
\ |
)*
$ Re
$
/X ){
Print "$1 match \ n ";
}

Related Article

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.