What are the Perl pattern matching options
This article mainly introduces what the Perl pattern matching options are, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.
Brief introduction of Perl mode
The Perl pattern refers to the characters of a specific sequence found in the string, contained by the backslash: / def/ is the Perl pattern def. Its use, such as combining the function split, divides the string into multiple words with a Perl pattern: @ array=split (/ /, $line).
Perl pattern matching options
Option description
G matches all possible Perl schemas
I ignore case
M treats strings as multiple lines
O assign a value only once
S treats the string as a single line
X ignore whitespace in Perl mode
1. Match all possible Perl modes (g option)
@ matches= "balata" = ~ / .a / Gbomaches.com ("ba", "la", "ta")
Matching loops:
While ("balata" = ~ / .a / g) {
$match=$&
Print ("$match\ n")
}
The result is:
Ba
La
Ta
When option g is used, the function pos can be used to control the offset of the next match:
$offset=pos ($string)
Pos ($string) = $newoffset
2. Ignore case (I option) example
/ de/i matches de,dE,De and DE.
3. Treat a string as multiple lines (m option)
In this case, the ^ symbol matches the beginning of a string or the beginning of a new line; the $symbol matches the end of any line.
4. perform a variable replacement example only once
$var=1; $line=; while ($var