In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to understand the Perl model, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The Perl pattern refers to the characters of a specific sequence found in the string, contained by the backslash: / def/ is the pattern def. It is used in conjunction with the function split to divide a string into multiple words in a certain pattern: @ array=split (/ /, $line).
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 pattern def. It is used in combination with the function split to divide a string into multiple words in a certain pattern: @ array=split (/ /, $line)
I. replacement operator for Perl mode
The syntax is replacement replacement, which has the effect of replacing the part of the string that matches the place. Such as:
$string= "abc123def"
$string=~s/123/456/;#now$string= "abc456def"
You can use the Perl mode order variable $n in the replacement section, such as s / (\ d +) / [$1] /, but in the replacement section, special characters that do not support Perl mode, such as {}, *, +, etc., such as s/abc/ [def] / will replace abc with [def].
The options for replacing operators are as follows:
Option description
G change all matches in Perl schema
I ignore case in Perl mode
E replace string as expression
M treats the string to be matched as multiple lines
O assign a value only once
S treats the string to be matched as a single line
X ignore whitespace in Perl mode
Note: the e option treats the string in the replacement part as an expression and calculates its value before replacing, such as:
$string= "0abc1"
$string=~s/ [a-zA-Z] + / $& x2bat e-0abcabc1
II. Translation operators of Perl mode
This is another alternative, syntax such as: tr/string1/string2/. Similarly, string2 is the replacement part, but the effect is to replace * characters in string1 with * characters in string2, the second character in string1 with the second character in string2, and so on. Such as:
$string= "abcdefghicba"
$string=~tr/abc/def/;#nowstring= "defdefghifed"
When string1 is longer than string2, its extra characters are replaced with * of string2; when the same character appears more than once in string1, * replacement characters are used.
The options for the translation operator are as follows:
Option description
C translate all unspecified characters
D Delete all specified characters
S reduces multiple identical output characters to one
For example, $string=~tr/\ dCompact c; replace all non-numeric characters with spaces. $string=~tr/\ t tab d; delete tab and spaces; $string=~tr/0-9 CharacterCharacter CS; replace other characters between numbers with a space.
Third, extended Perl pattern matching
Perl supports some Perl pattern matching capabilities that are not available in Perl4 and standard UNIXPerl pattern matching operations. The syntax is: (? pattern), where c is a character and pattern is the working Perl mode or sub-Perl mode.
1. Do not store the matching content in parentheses
In Perl's Perl mode, the child Perl schema in parentheses is stored in memory, which cancels the storage of matches in the parentheses, such as\ 1 in / (?: a | b | c) (d | e) f\ 1 / indicates a matched d or e instead of an or b or c.
2. Embedded Perl mode option
Usually the Perl mode option is followed by four options: I, m, s, x can be embedded, and the syntax is: / (? option) pattern/, is equivalent to / pattern/option.
3. Positive and negative foresight match
The positive foresight matching syntax is / pattern (? = string) /, which means to match the Perl pattern followed by string, on the contrary, (?! string) means to match the subsequent non-string Perl pattern, such as:
$string= "25abc8"
$string=~/abc (? = [0-9]) /
$matched=$&;#$& is the matched Perl mode, here it is abc, not abc8
4. Perl schema annotations
In Perl5, you can use? # in Perl mode to add comments, such as:
If ($string=~/ (? I) [amurz] {2jue 3} (? # matchtwoorthreealphabeticcharacters) / {
On how to understand the Perl model to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.