Regular expressions sort of knowledge of linux

1. Regular Expressions

1.1 regular expressions

  Regular expressions are a set of rules and methods for developers to handle large amounts of text string and defined using regular expressions can increase efficiency, quick access to the desired content.

  Regular expressions are used in the linux trio grep, sed, awk, for processing the text data, but also for normal commands.

  When the Three Musketeers to use regular expressions to process text, based on the behavior of the unit.

1.2 basic regular expressions and extended regular expressions

  Regular expressions and extended regular expressions and their meanings, as follows:

###### matching characters (only match a single character)
. Matches any single character (wildcard? Represent a single character)
[abc] Matches any single character set
[^abc] Matches any single character outside the set of (non-regular expression is not supported!,! Will be treated as a character)
###### number of matches (just the number, not the character representation)
*

Matches the preceding character any number of times (meaning how many times consecutive, the preceding character can not,

Match 0, ie empty, match all the contents)

.* Matches any character of any length, i.e. match all content
\? Character before a match 0 or 1 times (up to 1, the previous character optional), backslash is an escape
\+ Matching at least one preceding character times (or more times), backslash is an escape
a\{m\} Match before a character a, m times (a specific number), backslash is an escape
a\{m,n\} One character before a match at least m times, n times at most, it is the escape backslash
a\{0,n\} A character before a match at most n times, there may be no, 0 can be omitted, backslash is an escape
a\{1,n\} Before a match at least one character and at most n times, backslash is an escape
a\{m,\} One character before a match at least m times, not multiple restriction, backslash is an escape
###### position anchor
^ The first anchor line
$ End of line anchor
^PATTERN$ Pattern matching entire line (specifically string matching the whole line)
^$ Matching blank lines
^[[:space:]]*$ Matching blank (empty may not) or a blank line
\ <Or \ b Anchoring the first word (word left mode, a word may be a string or a number, not a special symbol)
\> Or \ b Suffix anchor (right word pattern, may be a word string or a number, not a special symbol)
\<PATTERN>\ Match whole words
###### grouping and references
\(\)

One or more characters tied together, as a whole process, such as \ (xy \) * matches in front of

The xy any number of times

\(ab\+\(xy\)*\) \1:  ab+\(xy\)*
\2:  xy

Repeat 1.2 1.3

### basic regular expressions: 
^ # <=== usage ^ keyword, keyword matching lines beginning with 
$ # <=== usage keyword $, ending with keyword matching line 
^ $ # <=== match empty OK 
. # <=== represent any single character 
\ # <=== escape character, to restore the original meaning of the characters 
* # <=== before the match a character 0 or more times (the previous character 0 times, matching All content) 
. * # <=== match all content 
^ * # <=== matches any number of characters at the beginning of the content 
. * $ # <=== match any number of characters at the end of content 
[abc] # <=== matching set within any single character 
[^ abc] # <=== outside any single character matching set 
### extended regular expressions (egrep used without adding \ escape): 
\ + # <= == previous character match once or more than once (at least once)	 
\ [: / \] + # <=== match in []: / (or other special characters) characters or once or more times ( at least 1) 
? \ # <=== previous character match 0 or 1 times 
| # <=== simultaneously a plurality of filter strings, using the separator (in a piping wildcard) 
\ (\) # <=== parentheses are contents as a whole, can be \ n references from the (n digital) 
\ n # <=== reference content (n is a number) () in 
a \ {m, n \} # <
a \ {n, \} # < === matches a character before a at least n times as many as not
a \ {n \} # <=== match the previous character n times 
a \ {, m \} # < === former matches a character at most m times, there may be no    

 

 

Guess you like

Origin www.cnblogs.com/blog-tim/p/11771583.html