Perl Regular Expressions
A regular expression describes a pattern for string matching; it can be used to check whether a string contains a certain substring, replace a matched substring, or extract a substring that meets certain conditions from a string.
The regular expression feature of the Perl language is very powerful, essentially the most powerful among commonly used languages; many languages have referenced Perl's regular expressions when designing regex support.
Perl's regular expressions have three forms: matching, substitution, and transliteration:
-
Matching: m/
Substitution: s/
Transliteration: tr/
These three forms are generally used in conjunction with the match operator =~ or !~ =~ indicates a match, while !~ indicates no match.
The match operator
The match operator m// is used to match a string statement or a regular expression. For example, to match "run" in the scalar $bar, the code is as follows:
Example
#!/usr/bin/perl
$bar
=
"
I am runoob site. welcome to runoob site.
"
;
if
(
$bar
=~
/
run
/
)
{
"
First match
\n
"
;
}
else
{
"
First no match
\n
"
;
}
$bar
=
"
run
"
;
if
(
$bar
=~
/
run
/
)
{
"
Second match
\n
"
;
}
else
{
"
Second no match
\n
"
;
}
Running the above program, the output is:
第一次匹配
第二次匹配
Pattern Match Modifiers
Pattern matching has some commonly used modifiers, as shown in the following table:
| Modifiers | Description |
|---|---|
| i | Ignore case in the pattern |
| m | Multi-line mode |
| o | Assign only once |
| s | Single-line mode, "." matches "\n" (by default it does not) |
| x | Ignore whitespace in the pattern |
| g | Global match |
| cg | After a global match fails, allow searching for the match string again |
Regular Expression Variables
After Perl processes a match, it stores the matched values in three special variables:
- **$`:** the string preceding the matched part
- **$&:** the matched string
- **$':** the remaining string that has not been matched
If you put these three variables together, you will get the original string.
The example is as follows:
Example
#!/usr/bin/perl
$string
=
"
welcome to runoob site.
"
;
$string
=~
m
/
run
/;
"
String before the match:
$`
\n
"
;
"
Matched string: $&
\n
"
;
"
String after the match:
$'
\n
"
;
The output of the above program is:
匹配前的字符串: welcome to
匹配的字符串: run
匹配后的字符串: oob site.
The substitution operator
The substitution operator s/// is an extension of the match operator, using a new string to replace the specified string. The basic format is as follows:
s/PATTERN/REPLACEMENT/;
PATTERN is the match pattern, and REPLACEMENT is the replacement string.
For example, we replace "google" with "runoob" in the following string:
Example
#!/usr/bin/perl
$string
=
"
welcome to google site.
"
;
$string
=~
s
/
/
runoob
/;
"
$string
\n
"
;
The output of the above program is:
welcome to runoob site.
Substitution Modifiers
The substitution modifiers are shown in the following table:
| Modifiers | Description |
|---|---|
| i | If "i" is added to the modifiers, the regex becomes case-insensitive, that is, "a" and "A" are the same. |
| m | By default, the regex start "^" and end "$" apply only to the whole string; if "m" is added to the modifiers, then start and end refer to each line of the string: the beginning of each line is "^" and the end is "$". |
| o | The expression is executed only once. |
| s | If "s" is added to the modifiers, then the default "." which represents any character except the newline becomes any character, including the newline! |
| x | If this modifier is added, whitespace characters in the expression are ignored unless they have been escaped. |
| g | Replace all matched strings. |
| e | Replacement string as an expression |
The transliteration operator
The following are the modifiers related to the transliteration operator:
| Modifiers | Description |
|---|---|
| c | Transliterate all characters not specified |
| d | Delete all specified characters |
| s | Squeeze multiple identical output characters into one |
The following example converts all lowercase letters in the variable $string to uppercase:
Example
#!/usr/bin/perl
$string
=
'
welcome to runoob site.
'
;
$string
=~
tr
/
a
z
/
A
Z
/;
"
$string
\n
"
;
The output of the above program is:
WELCOME TO RUNOOB SITE.
The following example uses /s to delete repeated characters in the variable $string:
Example
#!/usr/bin/perl
$string
=
'
runoob
'
;
$string
=~
tr
/
a
z
/
a
z
/
s
;
"
$string
\n
"
;
The output of the above program is:
runob
More examples:
$string =~ tr/\d/ /c; # 把所有非数字字符替换为空格
$string =~ tr/\t //d; # 删除tab和空格
$string =~ tr/0-9/ /cs # 把数字间的其它字符替换为一个空格。
More Regular Expression Rules
| Expression | Description |
|---|---|
| . | Match any character except the newline |
| x? | Match the string x zero or one time |
| x* | Match the string x zero or more times, but as few times as possible |
| x+ | Match the string x one or more times, but as few times as possible |
| .* | Match any character zero or more times |
| .+ | Match any character one or more times |
| {m} | Match the specified string exactly m times |
| {m,n} | Match the specified string between m and n times |
| {m,} | Match the specified string at least m times |
| [] | Match a character that fits within [] |
| [^] | Match a character that does not fit within [] |
| [0-9] | Match all digit characters |
| [a-z] | Match all lowercase letter characters |
| [^0-9] | Match all non-digit characters |
| [^a-z] | Match all non-lowercase-letter characters |
| ^ | Match the beginning of a string |
| $ | Match the end of a string |
| \d | Match a digit character, same as the [0-9] syntax |
| \d+ | Match a string of multiple digits, same as the [0-9]+ syntax |
| \D | Non-digit, otherwise same as \d |
| \D+ | Non-digit, otherwise same as \d+ |
| \w | A string of letters or digits, same as the [a-zA-Z0-9_] syntax |
| \w+ | Same as the [a-zA-Z0-9_]+ syntax |
| \W | A string of non-letters or non-digits, same as the [^a-zA-Z0-9_] syntax |
| \W+ | Same as the [^a-zA-Z0-9_]+ syntax |
| \s | Whitespace, same as the [\n\t\r\f] syntax |
| \s+ | Same as [\n\t\r\f]+ |
| \S | Non-whitespace, same as the [^\n\t\r\f] syntax |
| \S+ | Same as [^\n\t\r\f]+ |
| \b | Match a string bounded by letters or digits |
| \B | Match a string not bounded by letters or digits |
| a|b|c | Match a string that fits character a, or character b, or character c |
| abc | Match a string containing abc. (pattern) The () symbol remembers the found string 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. |
| /pattern/i | The i parameter means ignore letter case, that is, when matching strings, letter case is not considered. \ If you want to find a special character in the pattern, such as "*", you need to add a \ symbol before the character to disable its special meaning. |
More reference material
Regular expression:https://www.runoob.com/regexp/regexp-tutorial.html
Perl regular expression:https://perldoc.perl.org/perlre#Regular-Expressions