Shell Regular Expression

Source: Internet
Author: User

  1. basic regular expression

    metacharacters role
    * matches the previous character 0 or more times
    . match any character except newline characters
    ^ matches the beginning of the line. Reverse
    $ match line end
    [] matches any one of the characters set in brackets. For example: [0-9] matches any number. [A-z] matches any lowercase letter
    [^] matches any character except the bracket character. For example: [^a-z] matches any one of the non-lowercase letters
    \ escape character. For example: grep "\.$" test.txt matches with a. End of the line
    \{n\} means that the preceding characters appear exactly n times. For example, [0-9]\{4\} matches a 4-bit number. 1[3-8][0-9]\{9\} matches the phone number
    \{n,\} indicates that the characters preceding it appear no less than n times. For example: [0-9]\{2,\} matches a number with at least 2 digits or more
    \{n,m\} indicates that the characters preceding it appear at least n times and appear at most m times. For example: [a-z]\{4,6\} matches 4-bit to 6-bit lowercase letters

     

  2. Example

    grep "A *" test.txt Note: It matches the "a" that appears 0 or more times, and all the content is output

    grep "aa*" test.txt matches a row that contains at least one a

    grep "s.. D "Text.txt matches lines with 2 characters between S and D

    grep "S.*d" Text.txt matches any character between S and D

    grep ". *" Text.txt matches all content

    Grep-n "^$" text.txt match empty line and lose travel number

    grep "^[a-z" matches lines that start with lowercase letters

    grep "^[^a-za-z" matches lines that do not start with a letter

Shell Regular Expression

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.