In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to use regular expressions in Linux. 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.
Regular expression of Linux
The difference between regular expressions and wildcards:
The commands that most often apply regular expressions are grep (egrep), sed,awk.
There is an essential difference between regular expressions and wildcards. Regular expressions are used to find [file] content, text, and strings. Usually only three swordsmen support it. Wildcards are used to find: file name. All normal commands are supported.
Classification of regular expressions
The POSIX specification divides regular expressions into two types.
Basic regular expression (BRE,basicregularexpression)
Advanced features: extended regular expressions (ERE,extendedregularexpression)
The only difference between BRE and ERE is the metacharacter.
The only meta characters recognized by BRE (basic regular expressions) are ^ $. [] * other characters are recognized as ordinary characters:\ (\)
ERE (extended regular expression) adds () {}? + | etc.
The character () {} is treated as a metacharacter in BRE only if it is escaped with a backslash "\", while in BRE, any meta symbol preceded by a backslash causes it to be treated as a normal character.
Basic regular expression
Character description ^ ^ word: search for content that begins with word $word$: search for content ending with word ^ $represents a blank line, not a space. Represents and can only represent any character (does not match blank lines)\ escape character, let the special meaning character take off the vest, show the original shape, such as\. Only represents 0 or more characters or text before the decimal point * repeats the previous text or characters 0 or more times in a row. * any number of characters ^. * start with any number of strings,. * as many as possible, as many as possible. Greedy [^ abc] matching does not contain any character an or b or c after ^, which is an inverse of [abc]. And the meaning of an is different from ^. Repeat the previous a character n to m times (if you use egrep or sed-r to remove the slash)
A\ {n,\} repeat the previous a character at least n times, remove the slash a\ {n\} if you use egrep or sed-r, and remove the slash if you use egrep or sed-r
Extended regular expression ERE
Special character meaning and sentence + repeat the previous character one or more times, the previous character is consecutive one or more, take out the continuous text / character? Repeat the previous character 0 or 1 times (. Yes and only 1) pipe character | indicates or filters multiple characters at the same time () packet filter the enclosed thing represents a whole (one character)
Pipe character |
(): packet filtering what is enclosed represents a whole (one character)
The earliest text matching programs used basic regular expressions (BRE) defined by POSIX to match text.
Grep-E forces grep to recognize regular symbols directly, eliminating the need for escape
Egrep equivalent grep-E is born to recognize regular symbols.
We can make backup in the form of cp file name {, .bak} to avoid typing the file name again.
Sed-r: let sed support regular
The difference between basic regularity and extended regularity
Basic regular BRE extension regular ERE\? + +\ {} {}\ ()\
The so-called basic regularity is actually the regularity that requires escape characters to cooperate with the expression, and to extend the regular is to let the command extend its permissions so that he can recognize the regular expression symbols directly (directly supported by egrep,sed-r discipline awk)
Supplementary explanation
1 some predefined:
Regular expression description example [: alnum:] [a-zA-Z0-9] matches any alphabetic or numeric character [[: alnum:]] + [: alpha:] matches any alphabetic character (including uppercase and lowercase letters) [[: alpha:]] {4} [: blank:] spaces and tabs (landscape portrait) [: blank:] * [: digit:] matches any numeric character [[ : digit:]]? [: lower:] matches lowercase letters [[: lower:]] {5 } [: upper:] matches uppercase letters ([[: upper:]] +)? [: punct:] matches punctuation [[: punct:]] [: space:] matches a newline character All blank characters [[: space:]] + [: graph:] match any visible and printable character [[: graph:]] [: xdigit:] any hexadecimal number [[: xdigit:]] + [: cntrl:] any control character (the first 32 characters in the ASCII character set) [[: cntrl:]] [: print:] any printable character [[: print:]]
2 metacharacter
Metacharacter is a Perl-style regular expression. Only some text processing tools support it, not all text processing tools support it.
Example of regular expression description\ b word boundary\ bcool\ b matches cool, does not match coolant\ B non-word boundary cool\ B match coolant does not match cool\ d single numeric character b\ db matches B2B, mismatch bcb\ D single non-numeric character b\ Db match bcb does not match B2B\ w single word character (letter, number and _)\ w matches 1 or a, mismatch &\ W single non-word character\ W match Mismatch 1 or a\ nnewline character\ nmatch a new line\ s single white space character x\ sx matches xx, mismatch xx\ S single non-white space character x\ S\ x match xkx Do not match xx\ r enter\ r match enter\ t horizontal tab\ t match a horizontal tab\ v vertical tab\ v match a vertical tab\ f feed\ f match a page feed about "how to use regular expressions in Linux" this article ends here. 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.