Perl Regular Expressions

Source: Internet
Author: User

Matches: m/<regexp>;/ (can also be shortened to/<regexp>;/, omit m)
Replacement: s/<pattern>;/<replacement>;/
Conversion: tr/<pattern>;/<replacemnt>;/
These three forms are generally used in conjunction with =~ or!~ (where "=~" means matching, read as does in the entire statement, "!~" for mismatches, read as doesn ' t in the entire statement), and a scalar variable to be processed on the left. If you do not have the variable and the =~!~ operator, the default is to handle the contents of the $_ variable. Examples are as follows:

$str = "I love Perl";
$str =~ m/perl/; # indicates that if a "Perl" string is found in the $STR, return "1" or return "0".
$str =~ s/perl/bash/; # indicates that the "Perl" string in the variable $str is replaced with "BASH" and returns "1" If this substitution occurs, otherwise "0" is returned.
$str!~ tr/a-z/a-z/; # indicates that all uppercase letters in the variable $str are converted to lowercase letters and returns "0" if the conversion occurs, otherwise "1" is returned.

string = "I:love:perl";
$string =~ s/:/*/; #此时 $string = "I*love:perl";
$string = "I:love:perl";
$string =~ s/:/*/g; #此时 $string = "I*love*perl";
$string =~ tr/*//; #此时 $string = "I love Perl";
$string = "www22cgi44";
$string =~ s/(\d+)/$1*2/e; # (/d+) represents one or more numeric characters in the $string, which performs the operation of the numeric characters, so the last $string becomes "www44cgi88".


. Match all characters except newline  
x matches 0 or once x string  
x* matches 0 or more x strings, but matches the minimum possible number of times  
x+ matches 1 or more x strings, but matches can be The minimum number of times that can be  
. * Match any character   0 or one time;
. + Match any character   1 or more times;
{m} matches exactly the specified string of M  
{m,n} matches more than M n The specified string  
{m,} matches more than m of the specified string  
[] matches the character in []  
[^] matches the character   within [];
[0-9] matches all numeric characters  
[a-z] matches all lowercase alphabetic characters  
[^0-9] matches all non-numeric characters  
[^a-z] matches all non-lowercase alphabetic characters  
^ matches the character beginning with the character  
$ matches the character at the end of the character  
\d matches the character of a number, as in [0-9] syntax  
\d+ matches multiple numeric strings, 0-9]+ as   syntax,
\d non-numeric, others with \d 
\d+ non-numeric, others with \d+  
\w A string of letters or numbers, as in [a-za-z0-9] syntax  
\w+ and [a-za-z0-9]+ syntax  
\w non-English letters or numbers of strings, and [^a-za-z0-9] The syntax is the same as  
\w+ and [^a-za-z0-9]+ syntax  
\s spaces, like [\n\t\r\f] syntax  
\s+ and [\n\t\r\f]+  
\s non-spaces, and [ ^\N\T\R\F] syntax as  
\s+ and [^\n\t\r\f]+ syntax  
\b Matches string   with English letters, numbers as boundaries;
\b matches strings not in English letters, values are boundary  
A|b|c match A string that matches the A or B character or C character

The


example illustrates  
/perl/finds strings containing Perl  
/^perl/Find string   with Perl at the beginning;
/perl$/find string   with Perl at the end;
/ c|g|i/find a String containing C or G or I  
/cg{2,4}i/find C followed by 2 to 4 G, followed by the string I  
/cg{2,}i/find C followed by 2 or more g, followed by I string  
/cg{2}i/find C followed by 2 G, followed by the string I  
/cg*i/find C followed by 0 or more g, followed by a string of I, like/cg{0,1}i/ 
/cg+i/ Find C followed by more than a G, followed by the string I, like/cg{1,}i/ 
/cg?i/find C followed by 0 or 1 G, followed by the string I, like/cg{0,1}i/ 
/c.i/find C The face follows an arbitrary character, followed by the string I  
/C. i/find C followed by two arbitrary characters, followed by the string of I  
/[cgi]/Find a string that matches any one of these three characters  
/[^cgi]/Find a string that does not have any of these three words  
/\d/ Searching for characters that match numbers, you can use/\d+/to represent one or more numbers of strings  
/\d/search for characters that match not numbers, you can use/\d+/to represent one or more non-numeric strings  
/\*/Find matches * This character, because * in the regular expression has its special meaning, so to precede this special symbol with the \ symbol, so that the special character will be invalidated  
/abc/i find strings that match the ABC and do not take into account the case of these strings


Eight principles of regular expressions
If you have used the commands of SED, awk, and grep in Unix, you are not unfamiliar with the regular expressions (Regular expression) in the Perl language. Because of this function, the Perl language has a strong ability to handle strings. In the Perl language program, the use of regular expressions can often be seen, in CGI programming is no exception.

Regular expressions are a tricky part of learning Perl, but once you have mastered their syntax, you can have almost unlimited pattern-matching capabilities, and much of the work of Perl programming is mastering regular expressions. Here are some of the 8 principles used in the process of using regular expressions.

Regular expressions can form a huge coalition in the battle against data-often a war. We have to remember the following eight principles:

· Principle 1: Regular expressions have three different forms (match (m//), replace (s///eg), and conversion (tr///)).

· Principle 2: Regular Expressions match only scalars ($scalar =~ m/a/; @array =~ m/a/will treat @array as a scalar and therefore may not succeed).

· Principle 3: The regular expression matches the earliest possible match for a given pattern. By default, only the regular expression is matched or replaced once ($a = ' string string2 '; $a =~ s/string//; causes $a = ' String 2 ').

· Principle 4: Regular expressions can handle any and all characters that double quotes can handle ($a =~ m/$varb/Extend Varb to variables before matching, if $varb = ' a ' $a = ' as ', $a =~ s/$varb//; equivalent to $a =~ s/a//; The result of the execution is $a = "s").

· Principle 5: The regular expression in the evaluation process produces two cases: the result state and the reverse reference: $a =~ m/pattern/Indicates whether there is a substring pattern appearing in $a, $a =~ s/(word1) (WORD2)/$2$1/"swap" the two words.

· Principle 6: The core competencies of regular expressions are wildcard and multi-match operators and how they operate. $a =~ m/\w+/matches one or more word characters, $a =~ m/\d/"matches 0 or more digits.

· Principle 7: If you want to match more than one character set, Perl uses "|" to increase flexibility. If input m/(Cat|dog)/is equivalent to "match string cat or dog."

· Principle 8:perl provides extended functionality to regular expressions using (?..) syntax.

Reprint: http://www.cnblogs.com/cosiray/archive/2012/03/20/2408854.html

Perl Regular Expressions

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.