In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the regular expression in perl, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Regular expressions in perl
Three forms of regular expressions
First of all, we should know that there are three forms of regular expressions in Perl programs, which are:
Match: mCompare / (can also be abbreviated to / /, omitting m)
Replace: sUnix /
Converting: tr///
These three forms are generally used with = ~ or! ~, and the scalar variable to be processed on the left. If there is no such variable and the = ~! ~ operator, the default is to process the contents of the $_ variable.
(where "= ~" means match, read as does in the whole sentence, "! ~" means mismatch, and read as doesn't in the whole sentence)
Examples are as follows:
$str= "I lovePerl"
$str=~m/Perl/;# means that if the string "Perl" is found in $str, it returns "1" otherwise returns "0".
Str=~s/Perl/BASH/;# means to replace the "Perl" string in the variable $str with "BASH". If this replacement occurs, it returns "1", otherwise it returns "0".
$stringed letters tracker, Amurz str, Amurz etc. # means to convert all uppercase letters in the variable $string to lowercase letters, and to return "0" if the conversion occurs, or "1" otherwise.
There are also:
Foreach (@ array) {sqqr _ b _;} # where each loop takes an element from the @ array array and stores it in the $_ variable, and replaces the $_.
While () {print if (m/error/);} # is a little more complicated, printing all lines in the FILE file that contain error strings.
If () appears in the regular expression of Perl, the pattern in () is automatically assigned to the system $1 by the Perl interpreter after matching or replacing.
Take a look at the following example:
$string= "Ilove perl"; $string=~s/ (love) / /; # at this point $1 = "love", and the result of this replacement is to change $string to "Iperl" $string= "ilove perl"; $string=~s/ (I) (. *) (perl) / $2bat # where $1 = "I", $2 = "love", $3 = "perl", and after replacement $string becomes "love"
1. Pattern matching
1.1, matching operator
The matching operator m _ run / is used to match a string statement or a regular expression, for example, to match "run" in the scalar $string, the code is as follows:
Example
#! / usr/bin/perl $bar = "I am runoob site. Welcome to runoob site."; if ($bar = ~ / run/) {print "first match\ n";} else {print "first mismatch\ n";} $bar = "run"; if ($bar = ~ / run/) {print "second match\ n";} else {print "second mismatch\ n";}
Execute the above program, and the output is as follows:
The first match
The second match
1.2. Pattern matching modifier
Pattern matching has some common modifiers, as shown in the following table:
Modifier
Description
I
Ignore case in mode
M
Multiline mode
O
Assign a value only once
S
Single line mode, "." Match "\ n" (does not match by default)
X
Ignore whitespace in the pattern
G
Global matching
Cg
After a global match fails, the matching string is allowed to be found again.
1.3, regular expression variables
After perl processing, there are three special variable names for the matched values:
$`: the string that matches the first part of the part
$&: matching string
$': there is no remaining string that matches
If you put these three variables together, you will get the original string.
Examples are as follows:
Example
#! / usr/bin/perl $string = "welcome to runoob site."; $string = ~ string before matching: $`\ n "; print" matching string: $&\ n "; print" matching string: $'\ n "
The output result of executing the above program is:
String before matching: welcome to
Matching string: run
The matched string: oobsite.
2. Mode replacement
2.1. Replace operator
The replacement operator sswap / is an extension of the matching operator, replacing the specified string with a new string. The basic format is as follows:
S/PATTERN/REPLACEMENT/
PATTERN is the matching pattern and REPLACEMENT is the replacement string.
For example, we replace the "google" of the following string with "runoob":
Example
#! / usr/bin/perl $string = "welcome to google site."; $string = ~ print string\ n
The output result of executing the above program is:
Welcome to runoob site.
2.2. Replace operation modifier
The replacement operation modifier is shown in the following table:
Modifier
Description
I
If you add "I" to the modifier, the regular will remove case sensitivity, that is, "a" and "A" are the same.
M
The default regular start "^" and end "$" is only for regular strings if you add "m" to the modifier, then the beginning and end of each line will refer to each line of the string: each line begins with "^" and ends with "$".
O
The expression is executed only once.
S
If you add "s" to the modifier, the default is "." Means that any character other than a newline character will become any character, including a newline character!
X
If you add this modifier, the white space character in the expression will be ignored unless it has been escaped.
G
Replace all matching strings.
E
Replace the string as an expression
For example:
SpicActionAccord G means to replace all matching patterns in the string to be processed with strings.
SqqameUniPure e indicates that the part will be treated as an operator. This parameter is not used much.
For example, the following example:
$string= "i:love:perl"; $string=~s/:/*/;# now $string= "i*love:perl"; $string=~s/:/*/g;# now $string= "i*love*perl"; $string=~tr/*//;# this time $string= "iloveperl"; $string= "www22cgi44"; $string=~s/ (\ d +) / $1mm 2Unie # (/ d +) represents one or more numeric characters in $string and performs the operation of * 2 on these numeric characters, so finally $string becomes "www44cgi88".
3. Mode transformation
This is another alternative, syntax such as: tr/string1/string2/. Again, string2 is the replacement part, but the effect is to replace the first character in string1 with the first character in string2, the second character in string1 with the second character in string2, and so on. Such as:
$string = "abcdefghicba"; $string = ~ tr/abc/def/; # now string = "defdefghifed"
When string1 is longer than string2, its extra characters are replaced with the last character of string2; when the same character appears more than once in string1, the first replacement character is used.
For example:
#! / usr/bin/perl $str = "abbcddeff"; $str = ~ tr/abcc/ABCD/; print "$str\ n"
Output:
ABBCddeff
3.1. Conversion operator
The following are the modifiers related to the conversion operator:
Modifier
Description
C
Convert all unspecified characters
D
Delete all specified characters
S
Reduce multiple identical output characters to one
The following example converts all lowercase letters in the variable $string to uppercase letters:
#! / usr/bin/perl $string = 'welcome to runoob site.';$string = ~ tr/a-z/A-Z/; print "$string\ n"
The output result of executing the above program is:
WELCOME TO RUNOOB SITE.
The following example uses / s to remove the duplicate characters of the variable $string:
Example
#! / usr/bin/perl $string = 'runoob';$string = ~ tr/a-z/a-z/s; print "$string\ n"
The output result of executing the above program is:
Runob
More examples:
$string = ~ tr/\ d / / c; # replace all non-numeric characters with spaces $string = ~ tr/\ t / / d; # remove tab and spaces $string = ~ tr/0-9 / / cs # replace other characters between numbers with a space. A common pattern in regular expressions.
/ pattern/ result
. Matches all characters except newline characters
X? Match 0 or once x strings
X * match 0 or more x strings, but the least number of times possible
X + matches the x string one or more times, but as many times as possible
. * any character that matches 0 or more times
. + any character that matches one or more times
X {m} matches the specified string of m x
X {m direction n} matches specified strings greater than or equal to m and less than or equal to n x
X {m,} matches specified strings greater than or equal to m x
[] matches the characters in []
[^] match does not match the characters in []
[0-9] matches all numeric characters
[amurz] matches all lowercase characters
[^ 0-9] matches all non-numeric characters
[^ aMuz] matches all non-lowercase alphabetic characters
^ matches the character at the beginning of the character
$matches the character at the end of the character
\ d matches the character of a number, the same as the [0-9] syntax
\ d + matches multiple numeric strings, same as [0-9] + syntax
\ d is not a number, others are the same as\ d
\ D+ is not numeric, others are the same as\ d+
\ w A string of letters or numbers in English, the same as the [a-zA-Z0-9] syntax
\ W + is the same as [a-zA-Z0-9] + syntax
\ W A string that is not alphabetic or numeric, as in [^ a-zA-Z0-9] syntax
\ W+ is the same as [^ a-zA-Z0-9] + syntax
\ s space, same as [\ n\ t\ r\ f] syntax
\ s + is the same as [\ n\ t\ r\ f] +
\ s is not a space, same as [^\ n\ t\ r\ f] syntax
\ S+ is the same as [^\ n\ t\ r\ f] + syntax
\ b matches strings bounded by letters and numbers
\ B matches strings that are not bounded by letters and values
A | b | c matches strings that match a character, b character or c character
Abc matches a string containing abc
(pattern) () this symbol remembers the string you are looking for and is a very useful syntax. The string found in the first () becomes the variable $1 or\ 1, the string found in the second () becomes the variable $2 or\ 2, and so on.
The parameter / pattern/i I means that the case of English is ignored, that is, the case of English is not considered when matching strings.
\ if you are looking for a special character in pattern mode, such as "*", precede it with a\ symbol to invalidate the special character.
Here are some examples:
/ perl/ found the string containing perl
/ ^ perl/ found a string that begins with perl
/ perl$/ finds a string that ends with perl
/ c | g | I / find a string containing c or g or I
/ cg {2pr 4} I / find c followed by a string greater than or equal to 2 g, followed by a string of I
/ cg {2,} I / find c followed by 2 or more g, followed by the string of I
/ cg {2} I / find c followed by 2 g, followed by the string of I
/ cg*i/ finds c followed by 0 or more g, followed by a string of I, like / cg {0,} I /
/ cg+i/ finds c followed by one or more g, followed by a string of I, like / cg {1,} I /
/ cg?i/ finds c, followed by 0 or 1 g, followed by a string of I, like / cg {0SCR 1} I /
/ c.i/ finds c followed by an arbitrary character, followed by a string of I.
/ c..i/ finds c followed by two arbitrary characters, followed by a string of I.
/ [cgi] / find a string that matches any of these three characters
/ [^ cgi] / find a string without any of these three characters
/\ d / to find characters that match numbers, you can use /\ dcharacters / to represent a string of one or more numbers
/\ D/ looking for characters that match non-numeric characters, you can use /\ Dcharacters / to represent one or more non-numeric strings
/\ * / find the character that matches *, because * has a special meaning in regular expressions, so add the\ symbol before the special symbol in order to invalidate the special character
/ abc/I looks for strings that match abc and does not consider the case of these strings
For example, if we want to match something that does not start with #, we can write:
$string = ~ / ^ [^ #] /
If you want to match that the beginning is not # or /, you can write:
$string = ~ / ^ [^ #\ /] /
3. Principles of regular expressions:
Principle 1: regular expressions have three different forms (match (s///eg /), replace (tr///), and transform).
Principle 2: regular expressions only match scalars ($scalar=~m/a/; works; @ array=~m/a/ treats @ array as scalars, so it may not succeed).
Principle 3: regular expressions match the earliest possible match of a given pattern. By default, the regular expression is matched or replaced only once ($a matching stringstring2 matching regular expressions; causing $a matching string 2').
Principle 4: regular expressions can handle any and all characters that can be processed by double quotes ($a=~m/$varb/ extends varb to variables before matching; if $varb='a'$a='as',$a=~s/$varb//; is equivalent to $alpha varb='a'$a='as',$a=~s/$varb//;; the result is $a = "s").
Thank you for reading this article carefully. I hope the article "how to use regular expressions in perl" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.