Chapter 10th Shell Programming (1) _ Regular expressions

Source: Internet
Author: User
Tags lowercase

1. Basic Regular Expressions

1.1 regular expressions and wildcard characters

(1) A regular expression is used to match a string that matches a condition in a file , and a regular contains a match . grep, awk, sed, and other commands can support regular expressions.

(2) wildcards are used to match the qualified file names , and wildcards are exact matches . LS, find, CP These commands do not support regular expressions, so they can only be matched using the shell's own wildcard character.

1.2 Basic Regular Expressions

Metacharacters

Role

*

The previous character matches 0 or more times (is the previous character, which differs from Bash's wildcard characters)

.

Match any character except the line break

^

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

$

Matches the end of a line . For example: hello$ will match lines ending with Hello

[]

Matches any one of the characters specified in the brackets, matching only one character . For example: [AOEIU] matches any of the vowels, [0-9] matches any number, [a-z][0-9] matches a lowercase letter and a two-bit character consisting of one digit.

[^]

Matches any character except the characters in brackets . For example: [^0-9] matches any one non-numeric character, [^a-z] denotes any one non-lowercase letter.

\

The escape character. used to cancel the meaning of a special number .

\{n\}

Indicates that the preceding character appears exactly n times . is actually {n}, where {} is to be escaped for the original meaning.

\{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 characters preceding it appear at least n times and appear at most m times . For example: [a-z]\{6,8\} matches lowercase letters from 6 to 8 bits.

"Programming Experiment" regular expressions

Test text (Test_rule.txt)

Mr. Santa Claus Said:he was the "Honest Man" in HorzionStudio123 depise him. But since Mr. Rolling Stone came,he never saaaid those words.55555nice!because,actuaaaaly,mr. Rolling Stone is the most Ho Nest man! Later, Mr. Santa Claus soid his hot body.

(1) "*" means that the previous character matches 0 times, or any number of times.

① #grep "A *" test_rule.txt//Match all content, including blank lines. ( Understanding:* takes 0 o'clock, indicates a blank line or can be understood as a row where a is not present .) *≥1 indicates that a appears 1 or more rows . )

② #grep "aa*" Test_rule.txt//contains at least one line of a.

③ #grep "aaa*" test_rule.txt//A string containing at least two consecutive a

④ #grep "aaaa*" test_rule.txt//A string that contains at least four consecutive a.

(2) "." Match any character except the line break

①# grep "s.. D "test_rule.txt//matches a string that must have two characters between S and D.

②# grep "S.*d" test_rule.txt//matches any character between S and D letters.

③# grep ". *" Test_rule.txt//Match all content

(3) "^" matches the beginning of the line, "$" matches the end of the row

①# grep "^m" test_rule.txt//matches lines beginning with uppercase "M"

②# grep "n$" test_rule.txt//matches lines ending in lowercase n

③# grep-n "^$" test_rule.txt//matches a blank line,-n means the output is printed at the same time as the line number.

(4) "[]" matches any one of the characters specified in the brackets, matching only one character.

①# grep "S[ao]id" test_rule.txt//Match s and I letters, but not a, but O

②# grep "[0-9]" test_rule.txt//Match any number

③# grep "^[a-z]" test_rule.txt//matches a line that starts with a lowercase letter.

④# grep "^[^0-9]" test_rule.txt//matches lines starting with non-digits

(5) "\" Escape character

①# grep "\.$" test_rule.txt//matches with "." End of Line

(6) "\{n\}" indicates that the preceding character appears exactly n times.

①# grep "a\{3\}" test_rule.txt//matches a string of 3 consecutive occurrences of a letter, since the regular is a match, as long as there are 3 consecutive a in the string.

②# grep "[0-9]\{3\}" test_rule.txt//matches a string containing 3 numbers connected

(7) "\{n,\}" indicates that the preceding character appears not less than n times

# grep "^[0-9]\{3,\}[a-z]" test_rule.txt//matches a line that starts with a minimum of 3 consecutive digits.

(8) "\{n,m\}" matches characters that precede it at least n times, up to a maximum of two times m.

# grep "Sa\{1,3\}i" test_rule.txt//match between the letter S and I at least one a a maximum of 3 a

Chapter 10th Shell Programming (1) _ Regular expressions

Related Article

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.