Perl: Regular Expressions

Source: Internet
Author: User

Regular expression:

Regular expressions are called patterns in perl and are a template that matches a certain string.

Perl's regular expressions are similar to shell / awk / sed / grep.

 

m / pattern / Operation pattern matching:

/ pattern / is short for m / pattern /,

The delimiter // can be replaced with other symbols ([{<# $% etc.,

If the m at the beginning of // can be omitted, other symbols m cannot be omitted.

The delimiter should select characters that will not appear in the pattern.

 

Modifiers for regular expressions in Perl:

/ pattern / i Use i to match without case sensitivity.

/ pattern / s Use s to match any character.

/ pattern / x uses x to indicate that white space can be added to the pattern at will:

The white space in the pattern is ignored, you can use \ s to match the white space.

The comments in Perl are also blank.

 

if (m {

    Pattern1 # comment1

    Pattern2 # comment2

} six) {

    …

}

 

Perl back reference:

\ n indicates the reference to which group, and the group number is the position of the left parenthesis in //.

/(.)\1/ means to match two consecutive identical characters, \ 1 is a reference to the first group.

/x(...)y1\/ means that the three characters after x appear again after y, that is, x ... y ...

/x(.)(.)\2\1/ References to the first and second groups respectively, \ 2 refers to the reference to the second group.

/x((.)(.)\3\2)y\1/ uses three groups.

 

The new version of perl can use backreferences:

\ g {N} Use N to indicate which group.

 

The default matching object is $ _.

"String" = ~ / pattern / #Use the = ~ binding operator to specify matching objects.

$ res = <STDIN> = ~ / pattern /; #If it matches $ res = 1, otherwise it is empty

 

Capture variables:

$ n is used to indicate the content of the nth bracket in / pattern / from left to right.

The captured variables $ n will only be reset the next time the match is successful.

(?: Pattern) Use? : Indicates that this bracket is not captured, but only grouped, and $ n is not counted.

if ($ str = ~ / pattern /) {

    my $ match = $ 1; #Save the captured string for later use.

} else {

    … #If the match fails, $ n saves the last value.

}

 

The new version of Perl can use named capture:

Labeling method / (? <LABEL> PATTERN) /

The captured result enters a special hash% +

Key: label, value: just captured string.

LABEL names himself, the value of the visit is $ + {LABEL}

The new version of backreferences can also be used: \ g {LABEL} is equivalent to \ k <LABEL>

 

Match variables automatically:

$ & Is used to store the actually matched part in / pattern /.

$ `Is used to store the string before the matching start position.

$ ‘Saves the part of the string that has never been matched.

 

while (<>) {

    chomp;

    if (/ YOUR_PATTERN_GOES_HERE /) {

        print “Matched: | $` <$ &> $ ‘| \ n”;

    } else {

        print “No match: | $ _ | \ n”;

    }

}

 

s / pattern1 / pattern2 / Operation replacement operation:

s / pattern1 / pattern2 /; # Replace the content of pattern1 with pattern2.

s /// Replacement returns true, otherwise returns false.

The delimiter for the s operation can also be any character.

s # pattern1 # pattern2 #;

s {pattern1} {pattern2};

s [pattern1] <pattern2>;

 

Modifier of s replacement operation:

s / pattern1 / pattern2 / g; #g means global replacement.

s / ^ \ s / + | \ s + & // g; #Remove the leading and trailing blanks.

i means no case sensitivity

s means match any character.

m can match line breaks in the line

 

The default string for the s operation is also $ _, and operators can also be bound:

$ filename = ~ s # ^. * / ## s; #Remove the path to get the file name

 

Case conversion:

The following operations apply to strings within double quotes.

s / pattern1 / \ Upattern2 /; #Convert pattern2 to uppercase.

s / pattern1 / \ Lpattern2 /; #Convert pattern2 to lower case.

s / pattern1 / \ Upattern2 \ Epattern3 /; #Only convert pattern2 to uppercase.

s / pattern1 / \ Lpattern2 \ Epattern3 /; #Only convert pattern2 to lower case.

s / pattern1 / \ upattern2 /; #Capitalize the first character of pattern2.

s / pattern1 / \ lpattern2 /; #Lower the first character of pattern2.

 

Greedy quantifier:

Match as much as possible

*

+

?

{n}

{n,}

{n, m}

 

Non-greedy quantifier:

Match as little as possible

*?

+?

? ?

{n}?

{n,}?

{n, m}?

 

Update multiple files at once:

$ ^ I indicates the extension of the backup file.

 

Edit on the command line:

perl -p -i.bak -w -e ‘s / pattern1 / pattern2 / g’ fred * .dat

Change all pattern1 in all fred * .dat files to pattern2, and the backup file is * .bak.

 

~~ Smart match:

use 5.010;

Smart matching priority:

% a ~~% b

% a ~~ @b

% a ~~ / Fred /

% a ~~ ‘Fred’

@a ~~ @b

@a ~~ / Fred /

@a ~~ 123

@a ~~ ‘Fred’

$ name ~~ undef

$ name ~~ / Fred /

123 ~~ ‘123.0’

‘Fred’ ~~ ‘Fred’

123 ~~ 456

 

Automatically match keys in the hash:

say “I found a key matching‘Fred’. \ n ”

    if% name ~~ / Fred /;

 

Compare the size of two arrays:

say “The array have the sameelements! \ n”

    if @ arr1 ~~ @ arr2;

 

Determine whether the result is in the collection:

say “Ths $ result is one of the input values @nums! \ n”

    if @nums ~~ $ result;

 

split / PATTERN / [, EXPR, LIMIT]]:

Use the pattern as a delimiter to split the string into a list and return the list or list length,

split will keep the blank at the beginning and ignore the blank at the end,

Split splits $ _ by default.

[email protected] = split; # == split / \ s + /, $ _;

 

join EXPR, LIST:

Insert the string expr as a delimiter into the list to form a new string and return.

 

pos

quotemeta

study

qr //

perl: regular expression

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.