In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Linux regular expression". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Linux regular expression".
I. Linux text lookup command
Before talking about linux regular expressions, I also introduce three commands commonly used to find text files in linux:
1.grep: the earliest text matching program that uses basic regular expressions (BRE) defined by POSIX to match text.
2.egrep: extended grep that uses extended regular expressions (ERE) to match text.
3.fgrep: fast grep, this version matches fixed strings rather than regular expressions. And is the only version that can match multiple strings in parallel.
The grep command is briefly introduced as follows:
Syntax format:
Grep [options...] Pattern-spec [files...]
Purpose:
A line of text that matches one or more patterns.
Options:
-E: use extended regular expressions to match, grep-E or replace the egrep command.
-F: use a fixed string for matching, grep-F or replace the traditional fgrep command.
-e: usually the first non-optional parameter is considered the pattern to match, or you can provide multiple patterns at the same time, as long as you put them in single quotation marks and separate them with newline characters.
When the mode begins with a minus sign, in order to avoid confusion as an option, the-e option indicates that subsequent parameters are the mode, even if it begins with a minus sign.
-f: read the pattern from the pat-file file as a match.
-I: case differences are ignored in pattern matching.
-l: lists the file names that match the pattern instead of printing matching lines.
-Q: silent, if the match is successful, the matching line is not output to standard output; otherwise, it is unsuccessful.
-s: no error message is displayed and is usually used with-Q.
-v: displays lines that do not match the pattern.
Description: you can find the contents of multiple files at the same time, when you specify multiple files, each displayed file line will be preceded by a file name plus a colon to identify which file it comes from.
You can use multiple-e or-f options to establish a list of patterns to find.
two。 A brief introduction to regular expressions
1. The composition of regular expressions
(1)。 General characters: characters with no special meaning
(2)。 Special characters (meta characters): metacharacters that have a special meaning in regular expressions
two。 Here are the common meta characters in regular expressions
(1). Meta characters in both POSIX BRE and ERE:
\: usually used to turn on or off the special meaning of subsequent characters, such as\ (.\) and\ {.\}
. : matches any single character (except NUL)
*: matches any number or no single character before it, for example:. Represents any character, then. * matches any length of any character
^: matches the next regular expression. It only has a special meaning at the beginning of the regular expression in BRE, and it has a special meaning anywhere in ERE.
$: matches the previous regular expression, at the end of the string or line. BRE has a special meaning only at the end of the regular expression, and ERE has a special meaning anywhere.
[]: matches any character in formula brackets, in which the range of consecutive characters referred to by the hyphen (-) can be used; if the ^ symbol appears bitterly in the first position of the square brackets, it matches any character that is not in the list
(2) characters unique to POSIX BRE:
The interval expression that matches the difference in the number of times a single character precedes it. \ {n\} means to reproduce n times;\ {n ~ m\} means to reproduce n to m times
\ (\): space is reserved and up to 9 separate subschemas can be stored in a single schema. For example,\ (ab\). *\ 1: refers to two reproductions of matching ab combinations, with any number of characters in between.
\ n: repeat the pattern from the nth subpattern to this point in\ (and\) square brackets.
(3) characters unique to POSIX ERE:
{NMagol m}: same function as BRE's\ {NPM m\}.
+: matches one or more extensions of the previous regular expression
Match zero or one extension of the previous regular expression
| |: match | regular expression before or after the symbol |
(): matches a group of regular expressions enclosed in square brackets
(4)。 Square bracket ([]) expression
4.1. Character set [:]
There are several types of identity character sets:
[: alnum]: numeric character [: digit:]: numeric character [: punct:]: punctuation character [: alpha:]: alphabetic character [: graph:]: non-space character [: space:]: space character [: blank:]: space and positioning character [: lower:]: lowercase letter character [: upper:]: uppercase letter character [: Cntrl:]: control character [: print:]: displayable character [: xdigit:]: hexadecimal number
4.2. Sort symbol
Refers to the treatment of multiple characters as one symbol, such as [.ch.] Treat ch as a symbol
4.3. Equivalent character
It is considered that multiple characters are equal, such as [= e =] in French locale, can match a variety of characters similar to e, which are no longer listed here.
Note: these three constructs must be enclosed in additional square brackets in addition to their own square brackets.
Example: [: alpha:]!]: matches any English letter or exclamation point.
[.ch.]: matches the ch sort element, not the individual letter c or h.
3. Simple regular expression matching case
China: matches a line with china characters anywhere on this line
^ china: matches this line with the china switch
China$: matches lines ending in china
^ china$: matches lines with only five characters of china
[Cc] hina: matches rows with China or china
Ch.na: matches a line containing two letters of Ch followed by an arbitrary character followed by two characters of na
Ch.*na: the matching line contains Ch characters, followed by 0 or more characters, and then continues with na.
two。 Example
The following is used to learn the matching of BRE and ERE through common examples. The url.txt content of the source file is as follows:
Www.baidu.com
Http://www.baidu.com
Https://www.baidu.com
Http://wwwbaiducom
Baidu.com
Baidu
1.url matching
The match begins with http or https and is followed by: and contains. The string of
BRE matching:
Grep'^ https\ {0jue 1\}. *\.. * 'url.txt
ERE matching:
Grep-E'^ https?.*\.. * 'url.txt
The matching results are as follows:
Http://www.baidu.com
Https://www.baidu.com
2.Email matching
The contents of the sample file are:
Hfutwyy@qq.com
Aaaa@
Aaa@.com
Aaa@gmail.com
@ @ baidu.com
Matches multiple characters that begin with an alphanumeric or underscore, followed by an @ followed by multiple alphanumeric or underscore, including one. Number
Grep'^ [[: alpha:] [: digit:] _] * @ [[: alpha:] [: digit:]] *\.. * 'email.txt
Match result:
Hfutwyy@qq.com
Aaa@.com
Aaa@gmail.com
Stop here first, and then continue to write.
Thank you for your reading, the above is the content of "how to use Linux regular expressions". After the study of this article, I believe you have a deeper understanding of how to use Linux regular expressions, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.