In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use regular expressions in Perl", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use regular expressions in Perl" this article.
How to use regular expressions in Perl.
⑴ matching pattern
We know that regular expressions are called patterns in Perl, and this pattern (that is, regular expressions) can be placed in a delimiter consisting of pairs of symbols (for example, (), {}, etc.) or a pair of unpaired symbols (such as / /,!!, ^ ^, etc.), and the type of pattern is specified in lowercase letters before the delimiter. Of course, we don't want the delimiter to conflict with the symbol of the regular expression (you can escape with a backslash if there is a conflict). In fact, the most commonly used delimiter is the double slash / /. There are many processing patterns in Perl, of which the simplest one is the matching pattern m _ pattern, or it can be understood as a lookup pattern. Because the regular expression itself has a matching meaning, m can be omitted when the double slash is used as the delimiter. Other processing modes are detailed in the next section.
A simple example of Perl regular matching is as follows: $_ = "yabba dabba doo"; if (/ y (.)\ 2\ 1 /) {print "It matched!\ n";}
The running result is as follows:
The contents of the variable $_ are matched by default in the parentheses of if, so this program is actually shown as follows: $_ = "yabba dabba doo"; if ($_ = ~ / y (.) (.)\ 2\ 1 /) {print "It matched!\ n";}
Where = ~ is the binding operator that indicates the content match, and its return value is the Boolean value indicating whether the match is successful or not. Based on the above writing, we can change the name of the variable to match according to the actual need.
⑵ schema modifier
In addition to specifying the processing mode before the delimiter, you can add a lowercase letter modifier after the delimiter. These modifiers, also known as flag, are used to change the default matching behavior, as shown in the previous section using ASCII-encoded / a. The following modifiers are commonly used in matching patterns:
The different pattern modifiers in the table above can be combined, and their order has no effect on the pattern, as follows: / abc.*xyz/is # ignores case and makes the period match any character
⑶ anchor position
Starting with Perl 5, delimited ^ and $represent anchor positions at the beginning and end of a line, which is useful for line input data because the line input string has one and only one newline character at the end. For strings with multiple newline characters, that is, multiline text data variables, the / / m modifier can be used so that delimited ^ and $can simultaneously anchor the beginning of the string, the beginning of each line, the end of the string, and the end of each line, as follows: $_ = "This is the wilma linebarney is on another linebut this ends in fred"; if (/ ^ barney/m) {print "It matched!\ n" } if the / / m modifier is not added in the above code, ^ will only match the beginning of the string and the match will fail. In addition, there is another more rigorous method of anchoring, using\ A,\ Z,\ z to anchor the beginning of the string, the end of each line, and the end of the string. It is important to note that\ Z and\ z are also completely different for single-line strings entered on a line.\ Z matches the content before the newline character, while\ z matches the end of the string (including the newline character). Its usage is as follows: /\ Abarney/ # matches barney/fred\ z / # matches the absolute end of the string fred/fred\ Z / # matches the end of the line, that is, the fred/\ A\ s *\ Z / # before the newline character matches a blank line in addition to the string and the beginning and end of the line, the beginning and end of a word can be anchored using\ b The word here refers to the\ w character set, that is, the string composed of [a-zA-Z0-9 _].\ b determines the boundary of the word according to the occurrence of non -\ w characters (including the absolute beginning and end position of the string), as follows: /\ bfred\ b / # matches fred and fred's but not afred and fred_s
In addition,\ B anchors non-word boundaries, as shown below
/\ bfred\ B / # matches fred_s but not fred, fred's, afred
⑷ variable interpolation
Like the interpolation of variables inside double quotes, a variety of data variables can be used inside regular expressions to better integrate into Perl programs. Regular expressions generally enclose variables in parentheses (similar to backreferences), such as the following Mini Program similar to the grep tool command: my $what =; chomp $what;while () {if (/\ A ($what) /) {print "$_" }} in the above program, enter the value of $what through the keyboard, and the regular expression matches the beginning of each line of the file specified by the command line parameter according to the value of $what, and outputs the line if the match succeeds. $what can be any value, or even a regular expression metacharacter, as follows:
⑸ capture variable
In the pattern grouping of regular expressions in the previous section, we know that parentheses usually trigger regular expressions to capture matching strings for backreference. In fact, Perl automatically stores the capture groups in parentheses in scalar variables called capture variables, whose name is the same as the backreference number, and the name is the same as the capture group number, that is, $1, $2... . Capture variables as many parentheses as there are in the pattern, which can still be used after regular expression matching is complete, is one of the reasons for the power of Perl regular expressions. A simple example is as follows: $_ = "Hello there, neighbor"; if (/ (\ S+). *,\ s (\ w +) /) {print "What I said is:\ nroom1 $2!\ n";} the running result is as follows:
These capture variables are valid until the next regular expression is matched successfully. If a match fails, the data stored in the capture variable is still the data from the last successful match. The matching success here refers to the matching of the whole pattern rather than the matching of the capture group, which is why pattern matching and the use of capture variables are generally in Boolean control structures such as if and while. If you want to use the contents of a capture forever, you can use capture variables to assign values to custom scalar variables.
Although there are many ways to avoid confusing capture group numbers during program maintenance, such as using parentheses with pattern grouping only, using sequentially numbered capture variable names can still cause a lot of trouble. Starting from Perl 5.10, users are allowed to customize the naming of capture variables, called label, which is written by adding a hello at the beginning of the corresponding capture group brackets. And label, namely (? Regular expression). The final captured content will be stored in a special hash% +, whose key is label, and value is the content matched by regular expressions in parentheses. You can access the hash% + to use capture variables, and rewrite the previous program with custom label as follows: $_ = "Hello there, neighbor" If (/ (?\ S+). *,\ s (?\ w +) /) {print "What I said is:\ n + {name1} $+ {name2}!\ n";} the effect is the same as before. Similarly, backreferences in regular expressions can use\ g {label} or\ k {label}. In addition, Perl has three automatically captured variables, of which $& stores all the contents of the regular expression match, $`stores the content before the matching section, and $'stores the content after the matching section. These three capture variables can be used at will, but at the cost of slowing the program down. In Perl 5.10 and above, these three variables have another more vivid way to write ${^ PREMATCH}, ${^ MATCH}, and ${^ POSTMATCH}. These are all the contents of the article "how to use regular expressions in Perl". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.