In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what is Perl regular expression for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Perl regular expression
What is a Perl regular expression
A Perl regular expression is a formula that matches a class of strings with a certain pattern. Many people are afraid to use them because they look weird and complex-unfortunately, this article can't change that, but, after a little practice, I began to think that these complex expressions are actually quite easy to write, and, once you understand them, you can compress hours of painstaking and error-prone text processing in minutes (or even seconds). Perl regular expressions are widely supported by various text editing software, class libraries (such as RogueWave's tools.h++), scripting tools (such as awk/grep/sed), and interactive IDE like Microsoft's VisualC++ is beginning to support it.
We will use some examples to explain the use of Perl regular expressions in the following chapters, most of which are based on text replacement commands in vi and grep file search commands, but they are typical examples of concepts that can be used in sed, awk, perl, and other programming languages that support Perl regular expressions. You can take a look at the section on Perl regular expressions in different tools, where there are some examples of using Perl regular expressions in other tools. There is also a brief description of the text replacement command (s) in vi for reference.
II. The basis of Perl regular expression
Perl regular expressions consist of some ordinary characters and some metacharacters (metacharacters). Ordinary characters include uppercase and lowercase letters and numbers, while metacharacters have special meanings, which we will explain below.
In the simplest case, an Perl regular expression looks like a normal lookup string. For example, the Perl regular expression "testing" does not contain any metacharacters, which can match strings such as "testing" and "123testing", but not "Testing".
To really make good use of Perl regular expressions, the correct understanding of metacharacters is the most important thing. The following table lists all the metacharacters and a short description of them.
Metacharacter description
.
Matches any single character. For example, the Perl regular expression r.t matches these strings: rat, rut, rt, but not root.
$
Matches the line Terminator. For example, the Perl regular expression weasel$ can match the end of the string "He'saweasel", but not the string "Theyareabunchofweasels."
^
Matches the beginning of a line. For example, the Perl regular expression ^ Whenin can match the beginning of the string "Wheninthecourseofhumanevents", but not "WhatandWheninthe".
*
Matches 0 or more characters that precede it. For example, the Perl regular expression. * means that any number of characters can be matched.
\
This is a reference character that is used to match the metacharacters listed here as normal characters. For example, the Perl regular expression\ $is used to match the dollar sign, not the end of the line, and similarly, the Perl regular expression\. A wildcard used to match dot characters rather than any character.
[]
[c1-c2]
[^ c1-c2]
Matches any character in parentheses. For example, the Perl regular expression r [Aou] t matches rat, rot, and rut, but not ret. You can use a hyphen-in parentheses to specify an interval of characters, for example, the Perl regular expression [0-9] can match any numeric character; you can also set multiple intervals, for example, the Perl regular expression [A-Za-z] can match any uppercase and lowercase letter. Another important use is "exclude". To match characters outside the specified interval-- the so-called complement-- use the ^ character between the parentheses on the left and * characters. For example, the Perl regular expression [^ 269A-Z] will match any character except 2, 6, 9, and all uppercase letters.
\
The beginning of the matching word (word). For example, a better way to delete all blank lines in the Perl regular expression\ 0'price.txtawk
Awk'~/ ^ [JT] / 'price.txt prints the third field in all lines where the second field starts with' J' or'T'.
Awkpowered columns / [Mm] isc/ {print+} 'price.txt prints the sum of columns 3 and 4 (assuming numbers) for all rows that do not contain' Misc''or 'misc'' in the second field
Awkward fields / ^ [0-9] +\. [0-9] * $/ 'price.txt prints all lines where the third field is not a number, where a number refers to a form such as d.d or d, where d is any number from 0 to 9.
Awk'~/John | Fred/'price.txt prints the entire line if the second field contains' John' or 'Fred'
Grep
Grep is a program that uses RE to find in one or more files or input streams. Its name programming language can be used to process files and pipes. You can get complete information about grep in the manual. The same odd name comes from a command from vi, g/re/p, which means globalregularexpressionprint.
In the following example, we assume that the following text is included in the file phone.txt-- in the form of a last name plus a comma, then a first name, then a tab, then a phone number:
Francis,John5-3871
Wong,Fred4-4123
Jones,Thomas1-4122
Salazar,Richard5-2522
Grep command description
Grep'\ t5-...1'phone.txt prints out all phone numbers starting with 5 and ending with 1. Note that tabs are represented by\ t.
Grep' ^ S [^] * R'phone.txt prints all lines whose last name starts with S and first name starts with R
Grep' ^ [JW] 'phone.txt prints all lines whose last name begins with J or W
Grep',....\ t'phone.txt prints all lines with a last name of 4 characters, notice that the tab is represented by\ t
Grep-v' ^ [JW] 'phone.txt prints all lines that do not begin with J or W
Grep' ^ [Mmurz] 'phone.txt prints all lines with last names beginning with any character between M and Z
Grep' ^ [Mmurz]. * [12] 'phone.txt prints lines of all last names beginning with any character between M and Z and ending with a dot number of 1 or 2
Egrep
Egrep is an extended version of grep that supports more metacharacters in its Perl regular expressions. In the following example, we assume that the following text is included in the file phone.txt-- in the form of a last name plus a comma, then a first name, then a tab, then a phone number:
Francis,John5-3871
Wong,Fred4-4123
Jones,Thomas1-4122
Salazar,Richard5-2522
EgrepcommandDescription
Egrep' (John | Fred) 'phone.txt prints all lines containing the name John or Fred
Egrep'John | 22 $| ^ W'phone.txt prints all lines that contain John or end with 22 or W
Egrep'net (work)? s'report.txt finds all lines in report.txt that contain networks or nets
Perl regular expression syntax support
Command or environment. [] ^ $\ (\)\ {\}? + | ()
ViXXXXX
VisualC++XXXXX
AwkXXXXXXXX
SedXXXXXX
TclXXXXXXXXX
ExXXXXXX
GrepXXXXXX
EgrepXXXXXXXXX
FgrepXXXXX
PerlXXXXXXXXX
Brief introduction of vi replacement command
Replace command for Vi:
: ranges/pat1/pat2/g
Among them
This is the command execution interface of Vi.
Range is the specified scope of command execution. You can use a percent sign (%) to indicate all lines, a dot (.) to indicate the current line, and a dollar sign ($) to indicate a * * line. You can also use line numbers, such as 10 line 20 for lines 10 to 20,., $for the current line to * *,. + 2 for the last two lines of the current line to the penultimate line of the full text, etc.
S indicates that it is followed by a replacement command.
Pat1 this is a Perl regular expression to look for, and there are a lot of examples in this article.
Pat2 this is a Perl regular expression that wants to turn a matching string into a pattern, and there are a lot of examples in this article.
G optional flag, which means that the replacement will take place for each matching string in the line, otherwise only * * matching strings in the line will be replaced.
This is the end of this article on "what is Perl regular expression". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please 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.