About Perl Regular Expression Specification _ Basic Tutorial

Source: Internet
Author: User
Tags control characters lowercase modifier modifiers perl regular expression regular expression expression engine
1, delimiter
=~ m/there/;
=~ s/there/here/;
There are also types of delimiters:
=~/there/
=~ m#there#;
=~ s#there#here#;
=~ m (there);
=~ s (there) (here);
=~ M{there};
=~ S{there}{here};
=~ M[there];
=~ S[there][here];
=~ M,there,;
=~ S,there,here,;
=~ M.there.;
=~ S.there.here.;
=~ m|there|;
=~ s|there|here|;
=~ m ' There ';
=~ s ' there ' here ';
2, modifier
The modifier is generally placed after the last forward slash (or other delimiter) of the statement, and the modifier can be defined within the matching paradigm, which is implemented using the (? modifier).
/x allows annotation and extra whitespace characters to be added to the paradigm to improve the readability of the program.
/I allows matching paradigms that are case-insensitive.
The/S Single-line method, which determines the dot. Whether to match the newline character, using the/s, the dot will match the line break, otherwise it will not match.
Like what:
#!/usr/bin/perl
Use strict;
Use warnings;
My $string = ". \ n.";
Print "The original string is: $string \ n";
My $num = $string =~ s| (.)| #|sg;
Print "$num occurences change, and is changed to: $string \ n"; Exit
/m multiline mode, determines whether the caret ^ and dollar character $ matches the newline character, and if not/s,^ and $ can only be positioned at the beginning and end of the string, they do not match the embedded newline character, which is equivalent to \a and \z, or not just the start and end of the string. Also matches a position just before and after the embedded line break.
/O Only one time evaluates the value of an expression
/e replaces the string as an expression (valid only in the case of an alternative operation)
/g is a global modifier. In addition,/g and while use can be traversed in all matches of a string. Modifier/g must be used with/g to match the stop position of the previous/g match.
Such as
#!/usr/bin/perl
Use strict;
Use warnings;
My $string = "~32sda13daz. ' sda#!3_c-!";
Print "The original string is: $string \ n";
My $num = $string =~ s.\w.#.g;
Print "$num occurences change, and is changed to: $string \ n";
Exit
3, some special characters in the regular
() the Expression knot group
[] Looking for a set of characters
\d equals [0-9]
\d equals [^0-9]
\w equals [0-9a-za-z_]
\w equals [^0-9a-za-z_]
\s equals [\f\n\r\t]
\s equals [^\f\n\r\t]
. equals [^\n]
4, about some special symbols
\b does not belong to a white space character, indent one character forward
\ t is a blank character, matching tabs
\ r is a white-space character and matches a return character
\a does not belong to white space characters, matches the alarm clock character
\e is not a white space character, matching escape characters
\033 does not belong to a white space character, matching octal characters
\X1B is not a white space character, matching hexadecimal characters
\c[is not a white space character, matching control characters
White space characters, matching spaces
belongs to a blank character, matching tabs
\f belong to a blank character, matching a page break
\ n is a white-space character, matching newline characters
Not a white space character, the function is unknown
\c is not a blank character, the function is unknown
\x is not a blank character, the function is unknown
5, pay attention to the specificity of the selector in the regular
The selection operator is the lowest priority in all operators, which means it is finally executed.
6, some classical usages of the qualifier in the regular
Qualifiers are often used in conjunction with a number of characters or words
* Match any number;
+ Match one or more;
? Match 0 or one;
{n} matches N;
{N,m} matches n to M;
{n,} matches n and n above;
Qualifier greed seems to be innate. In the default state, the * or + qualifier matches the maximum number of instances of a paradigm that satisfies a regular expression. Available? The explicit definition of the qualifier is not greedy. If the question mark is placed after another qualifier (even after another question mark), you can make the qualifier not greedy.
7, Declaration and assertion
First notice that the length of the declaration is 0;
The Perl species has a set of statements that control case and code change:
\u the next letter to uppercase;
\l the next letter to lowercase;
\u the remaining characters of the text into uppercase;
\l The remaining characters of the text into lowercase;
\q can be replaced with characters other than letters until a \e declaration, a regular expression end, or a string end are encountered.
The \a Declaration and the caret (^) match the beginning of the string;
\Z Declaration and dollar sign ($) match the end of the string or the line break just before the end of the string;
\z only matches the end of the string;
\b Matches a word (word) boundary;
\b Matches a non word (word) boundary;
(? #text) ignores the explanatory text in parentheses;
(?:p Attern) is consistent with the group, but does not generate $1,$2 when matched;
(? imsx:pattern) is consistent with the group, but does not generate $1,$2 when matched, inline style matching modifiers during a particular style of validity;
(? =pattern) before you look at the declaration, if the regular expression matches the pattern style next time, it starts the match and does not affect the matching effect. such as/\w+ (? =\t)/will match whether the tabs appear immediately after a word \w+, and the tabs are not added to the $& value;
(?! If the regular expression does not match pattern at the back, the match will begin. such as/foo (?!) Bar)/, start matching only if Foo is present and bar is not followed;
After the statement, the following statement is matched only if the pattern already matches the following expression and does not place the result of pattern into the $& variable. If the/(? <=\t) \w+/matches the tab character to appear just before the \w+, but does not send tabs to the $&;
(? (? [Code]) Indicates that the use of code is experimental. If it returns true, it is considered to be a match with the (?:p Attern) assertion in the same row. The code does not insert a variable. This assertion is only valid in the use of ' eval ' compiled instruction Fu Shicai;
(? >pattern) If the type is locked in the current position, use a separate pattern to match the substring. If the regular expression/^ (? >a*) ab/never match, because the statement (? >a*) matches all the a characters at the beginning of the string and deletes the character a that matches AB;
(!<=pattern) To see the statement later, in contrast to the later statement meaning;
(!=pattern) A declaration, which is contrary to the previous view statement meaning;
(? (condition) yes-pattern|no-pattern) conditional expression-a conditional statement or an integer in parentheses, or an assertion;
(? (condition) Yes-pattern)
(? imsx) embed style matching modifiers. When you want to embed an expression modifier in a variable, and then use the variable in a general rule expression that does not specify its own modifier;
(?-imsx) This assertion is useful--anything that follows will close the modifier until another embedded modifier appears.
8, referencing backwards
Perl's regular expression engine allows you to use previously matched values, which are called backward references.
For example:
=~ m/(\w) \w* (\w) \w* (\w) \w* (\w) \w*\4\w*\3\w*\2\w*\1/;
=~ s/(\w) \w* (\w) \w* (\w) \w* (\w)/$4$3$2$1/;
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.