Regular expression (regular expression) describes a pattern of string matching, which can be used to check whether a string contains a certain substring, replace a matching substring, or extract a substring from a string that meets a certain condition.
Perl
regular expression function of the language is very powerful, which is basically the most powerful among the commonly used languages, and many languages can refer to it when designing regular expressions. `` Perl``gets or sets the regular expression.
The
Perl
three forms of regular expressions are matching, substitution, and transformation
Match:
m//(it can also be abbreviated as / /, omitting m)Replace:
s///Conversion:
tr///
These three forms are generally the same as Matching operator Execute the above program, and the output is as follows: 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” (default mismatch) X Ignore whitespace in the pattern G Global matching Cg After a global match fails, the matching string is allowed to be found again. 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: The output result of executing the above program is: Replace operator For example, we replace the “google” of the following string with “runoob”: The output result of executing the above program is: 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 ifyou add “m” to the modifier, then the beginning and end of each line willrefer to each line of the string: each line begins with “^” and ends with”$”. O The expression is executed only once. S If “s” is added to the modifier, the default “.” represents that any character other than the line break will become any character, including the line break! 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 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 sets the variable The output result of executing the above program is: The following examples use the The output result of executing the above program is: More examples: Expression. Description Matches all characters except newline characters Match 0 or once x strings Match 0 or more x strings, but as many times as possible Match one or more x strings, but the least number of times possible Any character that matches 0 or more times Any character that matches one or more times Matches a specified string that happens to be m Match specified strings with more than m and less than n Match more than m specified strings Match match The match does not match Match all numeric characters Match all lowercase characters Match all non-numeric characters Match all non-lowercase alphabetic characters Matches a character at the beginning of a character Matches the character at the end of the character Matches the character of a number, and [0-9] Grammar is the same Matches multiple numeric strings, and [0-9] + the same syntax Non-numeric, other same asd Non-numeric, other same asd + A string of letters or numbers, and And A string that is not a letter or number, and And Blank space, the syntax is the same as The syntax is the same as Non blank space, the syntax is the same as The syntax is the same as Match strings bounded by letters and numbers Match strings that are not bounded by letters and numeric values Match strings that match a character or b character or c character Matching a string containing abc (pattern) () this symbol remembers the string you are looking for and is a useful syntax. The string found in the first () becomes The parameter I ignores English case, that is, when matching strings, the case of English is not considered. if you are looking for a special character in pattern mode, such as
=~
or
!~
collocation use
=~
indicates a match
!~
indicates a mismatch. 5.39.1. Matching operator #
m//
used to match a string statement or a regular expression, for example, to match scalars
$bar
“run” in, 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";}
First match
Second match
5.39.2. Pattern matching modifier #
5.39.3. Regular expression variable #
Example #
#!/usr/bin/perl$string="welcome to runoob
site.";$string=~m/run/;print"String before matching:$\`\\n";print"Matched String:
$&\\n";print"Matched string:$'\\n";
String before matching: welcome to
Matching string: run
Matched string: oob site
5.39.4. Replace operator #
s///
is an extension of the matching operator to replace the specified string with a new string. The basic format is as follows:s/PATTERN/REPLACEMENT/;
PATTERN
is for matching pattern
REPLACEMENT
is the replacement string.Example #
#!/usr/bin/perl$string="welcome to google
site.";$string=~s/google/runoob/;print"$string\\n";
welcome to runoob site.
5.39.5. Replace operation modifier #
5.39.6. Conversion operator #
$string
convert all lowercase letters in to uppercase letters:#!/usr/bin/perl
$string = 'welcome to runoob site.';
$string =~ tr/a-z/A-z/;
print "$string\n";
WELCOME TO RUNOOB SITE.
/s
change the variable
$string
duplicate character deletion:Example #
#!/usr/bin/perl$string='runoob';$string=~tr/a-z/a-z/s;print"$string\\n";
runob
$string =~ tr/\d/ /c; # Replace all non numeric characters with spaces
$string =~ tr/\t //d; # Remove tabs and spaces
$string =~ tr/0-9/ /cs # Replace other characters between numbers with a space.
5.39.7. More regular expression rules #
.
x?
x*
x+
.*
.+
{m}
{m,n}
{m,}
[]
[]
characters within
[^]
[]
characters within
[0-9]
[a-z]
[^0-9]
[^a-z]
^
$
\d
\d+
\D
\D+
\w
[a-zA-Z0-9_]
grammar is the same
\w+
[a-zA-Z0-9_]+
grammar is the same
\W
[^a-zA-Z0-9_]
grammar is the same
\W+
[^a-zA-Z0-9_]+
grammar is the same
\s
[\n\t\r\f]
\s+
[\n\t\r\f]+
\S
[^\n\t\r\f]
\S+
[^\n\t\r\f]+
\b
\B
a|b|c
abc
$1
. This variable or
\1
variable, the string found in the second () becomes
$2
. This variable or2 variable, and soon.
/pattern/i
*
need to precede this characterwith
\
symbols, so as to invalidate special characters 5.39.8. More referenc #
Perl
Regular expressions: https://perldoc.perl.org/perlre#Regular-Expressions