Shell text filtering (regular expressions) __ Regular Expressions

Source: Internet
Author: User
Tags alphabetic character lowercase

When you extract or filter text from a file or command output, you can use regular expressions (R E), which are collections of special or not very special string patterns.

^ matches only the beginning of the line
$ only match end of line
* Only one single character followed by *, matching 0 or more of this single character
[] matches only the characters in []. Can be a single character, or it can be a sequence of characters. can be used-to represent the range of character sequences within [], as in the case of [1-5] instead of [1 2 3 4 5]
\ is used only to mask the special meaning of a single meta character. Because sometimes in S H e l l some meta characters have
Special meaning. \ can make it lose its meaning
. Match just any single character
P a t e r n \ {n \} is used only to match the occurrences of p a t T e R N in the front. n is the number of times
p a t t e R n \ {n,\} M only has the same meaning, but the least number is n
p a t t e R n \ {n,m \} only has the same meaning, but p a t e r n occurs in times between N and M

Period "." Match single character
1). : matches any single ASCII character, either as a letter, or as a number.
2) For example:.. XC.. Match dexc1t, 23XCDF, etc.,. W. W.. W. Matching rwxrw-rw-
To match a string or a sequence of characters at the beginning of a line
1 ^: Allows matching characters or words at the beginning of a line.
2) Examples: ^.01 matching 0011cx4, c01sdf, ^d matching drwxr-xr-x, drw-r--r--, etc.
End of line with $ match string or character
1) $: matches a string or character at the end of the line, and the $ symbol is placed after the matching word.
2) Example: trouble$ matches all lines ending with the word trouble
^$ matches all blank lines

Use * to match a single character in a string or its repeating sequence (unlike the "*" in the file name substitution)
1) *: A single character followed by the *, matching 0 or more of this single character.
2) Example: Compu*t will match character U one or more times, that is, match computer computing Compuuute etc.
1033* can match 101333 10133 1013444 etc
3 The use of "*" in regular expressions can sometimes produce unexpected results.

Use \ Mask The meaning of a special character
1) \: Used to mask the special meaning of a meta character. Because sometimes the meta characters have special meanings in the shell. \ You can make it lose its meaning.
2) Example: matches all files ending with *.pas in a regular expression: \*\.pas$

Use [] to match a single character belonging to a range or collection
1) []: matches the characters within "[]". Can be a single character, or it can be a sequence of characters. You can use "-" to denote the range of character sequences within the parentheses "[]".
In the case of [1-5] instead of [12345]. You can use commas "," to delimit the characters within the parentheses "[]".
2 when "^" symbol when directly leaning on "[", means negative or mismatched brackets "[]" in the contents
3) Example: [0-9] matches any number, [A-z] matches any one lowercase letter; [0-9a-za-z] matches any letter or number;
[C,c]omputer matching computer and computer;[^a-za-z] matches any non-alphabetic character

Number of occurrences of the pattern result using ' \{\} '
1) pattern\{n\}: The pattern of matching patterns occurs n times.
2) pattern\{n,\}: Match pattern patterns appear at least n times.
3) pattern\{,m\}: Match pattern patterns appear up to M times.
4) Pattern\{n,m\}: Match pattern patterns occur in the case between N and M.
5) Example: a\{2\}b matching value is AaB
A\{2,\}b matching values can be AaB or Aaaaab, but cannot match AB
A\{2,4\}b matching values can be AAB, Aaab, Aaaab, but cannot match AB or AAAAAB, etc.
[0-9]\{4\}cx[0-9]\{4\} match number 4 times followed by CX, and finally a number 4 times
6 actually the real format is {n} {n} {, m} {n,m}, except that the ESACPE character "\" is applied to "{" and "}".

Examples of regular expressions that are often used
[Ss]igna[ll] matches words SignaL, SignaL, SignaL, SignaL
[Ss]igna[ll]\. Ditto, but with a period
^user$ contains only the rows of the USER
\. Line with a period
^d.. X.. X.. x directories that have executable permissions for users, user groups, and other users, group members
^[^L] List of file directories after the symbolic link file (that is, rows that do not start with "L")
[Yynn] Uppercase or lowercase y or n
^.*$ matches any string in the row
^......$ includes 6-character lines
[A-za-z] Any single letter
[a-z]* at least one lowercase letter
[^0-9\$] Non-numeric or dollar sign
[123] 1 to 3 of a number
\^q starts with ^q.
^.$ line with only one character
^\. [0-9] [0-9] A line starting with a period and two digits
[0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\}
Date format dd-mm-yyyy
[0-9]\{3\}\. [0-9]\{3\}\. [0-9]\{3\}\. [0-9]\{3\} class IP address format

nnn.nnn.nnn.nnn
. * Match any number of characters

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.