Linux Regular expressions

Source: Internet
Author: User

Note: The regular expression is a matching string, and the wildcard is a matching file name

Regular expression rules:

* Previous character matches 0 or more times

. Match any character except the line break

^ matches the beginning of the line. For example: ^hello matches lines that begin with Hello

$ Match Line End

[] matches any one of the characters specified in the brackets, matching only one character

[^] matches any character except the characters in brackets, matching only one character

\ escape character, for cancellation, to cancel the meaning of the special symbol

\{n\} indicates that the preceding characters appear exactly n times. For example: [0-9]\{4\} means matching 4 digits

\{n,\} indicates that the preceding character appears not less than n times. For example: [0-9]\{2,\} represents two digits and above

\{n,m\} indicates that the preceding characters appear at least n times, up to M times. For example: [a-z]\{6,8\} matches lowercase letters from 6 to 8 bits

Example: (A.txt is to find the file used)

grep "A *" a.txt #表示匹配文件a all content in. txt, including blank lines

grep "aa*" A.txt #表示匹配至少含一个a的行

grep "aaa*" A.txt #表示匹配至少含两个aa的行

grep "s.. D "A.txt #表示匹配s和d之间有两个字符的行

grep "S.*d" A.txt #表示匹配只要包含s和d就行

grep "\.$" A.txt #表示匹配以. The end of the line, where the backslash is to be canceled. Special meaning

Linux Regular expressions

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.