Regular Expressions in Perl

Source: Internet
Author: User
Tags control characters expression engine

1. delimiters
= ~ 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 behind the last forward slash (or another separator) of the statement. The modifier can also be defined within the matching paradigm (? .
/X allows adding comments and additional spaces in the paradigm to improve Program Readability.
/I supports case-insensitive matching paradigms.
The/s single line method determines whether or not the dot matches the line break. If/s is used, the dot matches the line break; otherwise, the dot does not match.
For example:
#! /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 be changed to: $ string \ n"; exit;
The/m multi-line method determines whether the font size ^ matches the line break with the US $. If the/s character is not used, the ^ and $ values can only be located at the start and end of the string, they do not match the embedded line breaks, which are equivalent to \ A and \ Z. Otherwise, they not only match the start and end of the string, but also match exactly before and after the embedded line breaks.
/O calculate the expression value only once
/E uses the replacement string as an expression (only valid in the Replacement Operation)
/G is a global modifier. In addition, the/g and while functions can be traversed in all matching strings. The modifier/G must be used with/g to match the stop position of the previous/g match.
For example
#! /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 be changed to: $ string \ n ";
Exit;
3. Some special characters in the Regular Expression
() Grouping expressions
[] Searching for a group of characters
\ D equals to [0-9]
\ D equals to [^ 0-9]
\ W equals to [0-9a-za-z _]
\ W equals to [^ 0-9a-za-z _]
\ S equals to [\ f \ n \ r \ t]
\ S equals to [^ \ f \ n \ r \ t]
. Equal to [^ \ n]
4. Some special symbols
\ B is not a blank character and indented one character forward
\ T is a blank character and matches the tab
\ R is a blank character and matches the carriage return.
\ A is not a blank character and matches the Alarm Card
\ E is not a blank character and matches the Escape Character
\ 033 is not a blank character and matches the octal character
\ X1b is not a blank character and matches a hexadecimal character
\ C [not blank characters, matching control characters
It is a blank character and matches with spaces.
It is a blank character and matches the tab
\ F is a blank character and matches the Page Break
\ N is a blank character and matches a linefeed.
\ 0 is not a blank character and its function is unknown
\ C is not a blank character and its function is unknown
\ X is not a blank character and its function is unknown
5. Pay attention to the special character selection in the regular expression.
The selection operator has the lowest priority among all operators, which means it is last executed.
6. Classic usage of the qualifier in the Regular Expression
A qualifier is often used together with some characters or words.
* Match any number;
+ Match one or more;
? Matches zero or one;
{N} matches N;
{N, m} matches n to m;
{N ,}match N and N or more;
The qualifier seems to be inherent in greed. By default, the * or + qualifier matches the maximum number of instances of a regular expression. Available? Explicitly specifying the limit is not greedy. If the question mark is placed after another qualifier (or even after another question mark), the qualifier can be left ungreedy.
7. Declaration and assertions
Note that the declared length is 0;
Perl has a set of statements that control case sensitivity and code change:
\ U converts the next letter to uppercase;
\ L to lower the next letter;
\ U converts the remaining characters of the text to uppercase;
\ L converts the remaining characters of the text to lowercase letters;
\ Q performs code replacement for characters other than letters until \ e Declaration, regular expression, or string ends.
\ A declare and strip (^) match the start of the string;
\ Z and the dollar sign ($) match the end of a string or a line break that is just before the end of a string;
\ Z only matches the end of the string;
\ B matches the boundary of a word (word;
\ B matches a non-word boundary;
(? # Text) Ignore the comment text in brackets;
(? : Pattern) is consistent with the group, but $1, $2 is not generated when matching;
(? Imsx: pattern) is consistent with the group, but $1 and $2 are not generated during Matching. during the validity period of a specific style, the embedded style matching modifier is used;
(? = Pattern) See the Declaration before. If the regular expression matches the pattern style in the next time, it starts matching and does not affect the matching effect. For example,/\ W + (? = \ T)/whether the matching tab appears exactly behind the word \ W +, and the tab is not added to the $ & value;
(?! Pattern) if the regular expression does not match pattern later, the matching starts. For example,/Foo (?! Bar)/, starts matching only when Foo appears and bar is not followed;
(? <= Pattern). The following statement is matched only when pattern matches the following expression and the result of pattern is not included in the $ & variable. For example /(? <= \ T) \ W +/whether the matching tab appears right before \ W +, but the tab is not sent to $;
(? (? [Code]) indicates that the use of code is experimental. If the returned result is true, it is considered to be (? : Pattern) Assertion match in the same row. Code does not insert variables. This assertions are valid only when the use re 'eval' command is used to compile the indicator;
(?> If the type is locked at the current position, use a separate pattern to match the substring. Such as regular expression/^ (?> A *) AB/will never match because the statement (?> A *) matches all the characters starting with the string and deletes the character that matches the AB character;
(! <= Pattern) if it is not later, it means the opposite to later;
(! = Pattern) the statement is not previewed, which is the opposite of the statement;
(? (Condition) yes-pattern | no-pattern) condition expression-a condition statement, an integer in parentheses, or an asserted;
(? (Condition) yes-pattern)
(? Imsx) embedded style matching modifier. When the expression modifier is embedded in the variable, then the variable is used in a general rule expression that does not specify its own modifier;
(? -Imsx) This assertion is useful-the modifier will be closed after any content appears until another embedded modifier appears.
8. Backward reference
Perl's Regular Expression Engine allows the use of previously matched values, which are called backward references.
For example:
= ~ M/(\ W) \ W * (\ W) \ W * \ 4 \ W * \ 3 \ W * \ 2 \ W * \ 1 /;
= ~ S/(\ 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.