The use of regular expressions in Perl

Source: Internet
Author: User
Tags character set join lowercase reference set set split uppercase character

"." to match wildcard characters for any one character, except for "\ n" line breaks

"*" is used to match previous entries 0 or more times

"+" is used to match previous entries appearing more than once

"?" to match one or more occurrences of the preceding entry.

"()" is used to group strings, \1 or \2 can reverse-reference the strings in parentheses, the corresponding numbers represent the corresponding order of capturing groups, to invoke the capturing group in (), either capture it in sequence, or capture it with the $+{label} tag

01./(.) The \1/expression matches 2 consecutive characters, such as the ' AA ' 02./(\s) \1/matching a non-empty character 03./(.) that occurs 2 consecutive times. (.) \2\1/can match ' Abba ' 04./(.) \G{1}11/can use \g{n},n as a number to represent a reverse reference, to eliminate ambiguity 05./(?: Bronto)? Saurus (Steak|burger)/parentheses?: Indicates that the contents of the parentheses are not captured 06./(? <name1 >\w+) (?: And|or) (? <name2>\w+)/references to capturing groups with?<label> for automatically capturing variables, you can use $& to represent matching strings, $ ' to represent the fields before the string matches, $ ' Represents a field after a string match, or you can use ${^match},${^prematch},${^postmatch} separately

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Escape metacharacters, to match the "." Number itself, you need to use "\" escape, "\.", and "\" to match, then "\"

"|" means either match the front, or match the back

01./wilma.*fred|fred.*wilma/matches both Wilma and Fred in a row, the order in which they appear is considered "[a-za-z]" to denote a contiguous character set

01./[a-z][a-z]+/a string "\d" that matches the lowercase of the first letter in a row indicates the character set of any number, provided that in the ASCII character set, instead, "\d" denotes a negative meaning, representing a non-numeric

"\s" matches any whitespace, whereas "\s" denotes a matching non-white-space character

"\w" and "[a-za-z0-9_]" character sets, which represent word characters, whereas "\w" denotes non-word characters

Pattern-matching characters "//", in fact equivalent to "m//", "m//" can replace "m##", "m<>" and so on

The "$_" variable is matched in the default pattern match

"//i", I represents pattern matching for unrelated case

01./fred/i "//s", the point number in the S default conversion mode is the character set [\d\d], so the dot can match the line feed

01./barney.*fred/s If there is a newline between Barney and Fred, the match "//x" is not affected, X allows any whitespace to be inserted into the pattern to increase readability

01./-? [0-9]+ \.? [0-9]*/x "//a", representing pattern matching in ASCII character set

01./\W+/A represents the [a-za-z0-9_] Character Set "//u", which indicates a pattern match in the Unicode character set

01./\w+/u represents the character "//l" defined in Unicode as a word, representing the pattern matching of the character set set by localization

01./\W+/L represents the locale localized set of character set "\a" berth the absolute beginning of the matching string,

01./\astring/"\z" berth match the absolute end of the string, "\z" allows line breaks to occur later

01./string\z/"//m" can match multiple lines of string, and "$", "^" in conjunction with

01./fred$/m matches the "\b" word berth in a multiline string that matches the end of Fred in a multiline string 02./^barney/m the beginning of Barney.

01./\bfred\b/match a separate word Fred, which cannot be combined with other characters "=~" binding operators, specify strings and patterns to match, and default to match $_ variables without binding operators

01.string =~/expr/can specify the number of matches

01./a{3,15}/match a appears 3 to 15 times 02./fred{3,}/matching Fred appears with more than 3 alternate usage examples

01.$_ = "Green scaly dinosaur"; 02.s/(\w+) (\w+)/$2, $1/; 2 strings will be swapped for position, after the "scaly, green dinosaur" 03.s/^/huge,/; Insert huge at the beginning, change into "huge,scaly,green dinosaur" 04.s/,.*een//; Remove all characters from the first comma to green, and change to "huge dinosaur" 05.s/\w+$/($ '!) $&/; Replace to "huge" (huge!) Dinosaur "06.s/\s+ (!\w+)/$1/; Replace into "huge (huge!) dinosaur" 07.s/huge/gigantic/; Replace to "gigantic (huge!) dinosaur" s///g,/g for global replacement

Case substitution

01.$_ = "I saw Barney with Fred" 02.s/(barney| Fred)/\u$1/gi Barney and Fred are all replaced with uppercase character 03.s/(barney| Fred)/\l$1/gi Barney and Fred are all replaced with lowercase characters 04.s/(\w+) with (\w+)/\u$1\e with $2/\u only uppercase substitution, plus \e replace the split operator with uppercase instead of the following string. You can extract a string separated by a specific delimiter in a field and save it to a list variable

01.my @buddy = Split/:/, "Tom:jerry:nico:kid"; The list @buddy = ("Tom", "Jerry", "Nico", "Kid") $some _string = "This is\ta\t\ttest"; For fields separated by multiple whitespace 03.my @str1 = Split/\s+/, $some _string; You can extract a string join function on a field using the \s+ representation separator, inserting the specified delimiter into a different string

01.my $str 2 = join "-", 2,3,4,5,6; The resulting variable $str2 as "2-3-4-5-6"

This article is from the "Strive for" blog, please be sure to keep this source http://carllai.blog.51cto.com/1664997/1176116

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.