Linux Shell Regular Expression

Source: Internet
Author: User

In the Linux Shell environment, two regular expression rules are provided. One is the basic regular expression (BRE), and the other is the extended regular expression (ERE ).
The syntax list of these two expressions is as follows. Note that, if meta characters are not explicitly stated, they can be used for both bre and ere; otherwise, they will apply to the specified mode.

Regular metacharacters Meaning Use Cases
\ It is usually used to disable the special meaning of subsequent characters and restore the original intention. \ (... \), The brackets here only represent parentheses.
. Match any single character. A. B will match ABB, ACB, etc.
* Match a single character between 0 and N. A * B will match AB, AAB, aaab, etc.
^ Match the followed regular expression at the beginning of the row. ^ AB, which matches ABC, Abd, and so on, but does not match cab.
$ Matches the followed regular expression at the end of the row. AB $, will match AB, cab, etc., but does not match ABC.
[...] Square brackets match any internal characters. -Indicates the range of consecutive characters. If the ^ symbol is placed in square brackets, the first character has a reverse meaning, that is, it matches any character not in the list (square brackets. If you want] and-to indicate the original intention, You need to place them at the first character position of square brackets, such as [] AB] or [-AB]. If both characters exist at the same time, place] at the beginning and end, for example, [] AB-]. [A-bA-Z0-9!] All uppercase/lowercase letters, numbers, and exclamation points. [^ ABC] indicates all characters other than A, B, and C. [TT] om, which can match Tom and Tom.
\ {N, m \} An interval expression that matches the number of occurrences of a single character before it. \ {n \} indicates n times of repetition; \ {n, \} indicates at least N times of repetition; \ {n, m \} indicates repeated n to m times. AB \ {2 \} indicates abb; AB \ {2, \} indicates ABB and abbb. AB \ {2, 4 \} indicates ABB, abbb, and abbbb.
\(...\) The mode between parentheses is stored in a special "reserved space ". A maximum of nine independent sub-modes can be stored in a single mode. Text that matches the sub-mode can be reused in the same mode through escape sequence \ 1 to \ 9. \ (AB \). * \ 1 indicates that the AB combination appears twice, and there can be any number of characters between the two, such as abcdab and Abab.
{N, m} (ERE) The function is equivalent to the above \ {n, m \}, but the \ escape character is no longer written. AB + matches AB and abbb, but does not match.
+ (ERE) Compared with the asterisk, + matches 1-N instances of the regular expression.
? (ERE) Match the First Regular Expression with 0 or 1. AB? Only a or AB is matched.
| (ERE) Match with | the regular expression before and after the symbol. (AB | cd) matches AB or CD.
[: Alpha:] Match letter characters. [[: Alpha:]!] AB $ matches cab, dab, and! AB.
[: Alnum:] Matches letters and numbers. [[: Alnum:] AB $ matches 1AB and AAB.
[: Blank:] Matches spaces and tab characters. [[: Alnum:] AB $ matches 1AB and AAB.
[: Cntrl:] Match control characters.
[: Digit:] Match numeric characters.
[: Graph:] Matches non-space characters.
[: Lower:] Match lowercase letters.
[: Upper:] Match uppercase letters.
[: Punct:] Match punctuation characters.
[: Space:] Matches whitespace characters.
[: Xdigit:] Match the hexadecimal number.
\ W Match any character consisting of letters and numbers, equivalent to [[: alnum:] _]
\ W Match any character consisting of non-letters and numbers, equivalent to [^ [: alnum:] _]
\ <\> Match the start and end of a word. \ <Read matches README, me \> matches readme.


The following lists the types of Regular Expressions supported by common tools or commands in Linux Shell.

  Grep Sed VI Egrep Awk
Bre * * *
Ere * *

From: http://www.atatech.org/article/detail/2894/338

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.