A Perl extended regular expression Code Analysis _perl

Source: Internet
Author: User
Copy Code code 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 description of the PERLRE documentation, 1.1 point explanation. The first is/x, use this to remove the space in the Regex, otherwise write in a line too ugly to understand; then it is ^, which begins at the beginning; and then is (?:) This means that this bracket is not recorded in the reverse reference $&; d{1,3}.) {3}\d{1,3}), same inside one (?:, that is, this line matches an IP and is counted as $; =, this representation must be followed by the preceding line of IP in accordance with the definition of the parentheses, also excluding $& (the term "0-wide positive forward assertion" is it?) And then a separate IP |, and then the (?!), which means that nothing in the parentheses is allowed to appear, nor does it count into $& (the term "0 wide negation forward assertion" is it?). And then the \1, which is the previously captured $, combined with the assertion that the uplink is not followed by an IP duplicate of the previous match; 1, this means that the previous capture of the regular expression, that is, the case of no repeat IP, continues to capture the new IP; Front of (?:, that is, |IP can repeat multiple; then \z, this is a string boundary, equivalent to the role of $ in a single line, in this case can be interchangeable, used here, is to let (?!) \1) The check is performed to the last; =; then is | and), which is closed to ^ (, which means that the ip| format that matches the non repeating IP condition is constantly matched; 1 $, define the last IP, use and the same regular, that is, the string must have at least one IP. OK, the explanation is complete. In fact, from the forward look, but clear some ~ ~ another: Perlre in the (?? {CODE}) Paragraph is described in the following phrase "in Perl 5.12.x and earlier, because the Regex engine is not re-entrant, delayed code could not safely invoke The Regex engine either directly with "m//" or "s///"), or indirectly with functions such as "split". "and (? R) and (?? {CODE}) This is a similar and simple task, so if the Perl version of the Linux distribution is not high enough, this is not a simple way to write (1), and it needs to be written again. You can judge this:
Copy code code 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 "$ 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.