Basic Regular expression: REGular expression (REGEXP)
One, meta-character:
. : matches any single character;
[]: matches any single character within the specified range;
[^]: matches any single character outside the specified range.
PS : Common character sets
[:d igit:]--Digital Collection
[: lower:]--Small Letter Set
[: upper:]--Caps Collection
[: space:]--Space
[: alpha:]--All letters set
[: alnum:]--All letters and numbers set
Second, the number of matches:
*: Match the characters in front of it any time;
. *: Matches any character of any length;
\? : matches the characters in front of it at least once;
\{m,n\}: Matches its preceding character at least m times, up to N times.
PS : The regular expression defaults to working in greedy mode, which matches the maximum length string for an expression.
Three, the position anchor nails:
^: Anchor at the beginning of the line, the content after this character must appear at the top;
$: Anchor Line end, this character before the content must appear at the end of the row;
^$: Blank line (the line at which the anchor is just beginning to end);
\< or \b: Anchor word, any character behind it must appear as the first word (non-English word);
\> or \b: anchor ending, any character in front of which must appear as the tail of a word (non-English word).
For example,:\<root\> matches the word root.
Four, group:
\ (\): Matches the grouped content, matching the contents of the parentheses as a whole.
For example: \ (ab\) * means matching "ab" any time.
A back reference (someone also referred to as a forward reference): Used with grouping.
\1--refers to the first opening parenthesis and all content contained in the corresponding closing parenthesis (whether or not nested in parentheses)
\2--refers to the second opening parenthesis and all the contents of the corresponding closing parenthesis (IBID.)
\3 —— ... (An analogy) ...
For example, the following three lines of content:
He like his liker.
She Love her lover.
He like her lover.
Expression: L.. E can match each line;
Expression: \ (L.. e\). *\1 can only match the first two lines.
Extended Regular expression: (differs from basic regular expression)
? : matches the number of characters in front of it at most once (note: there is no \ in front of it);
+: Matches the number of characters before it at least once (note: The basic regular expression does not have this metacharacters);
{M,n}: Number of times match, usage with basic regular expression (no need to use \ in front of curly braces);
(): Grouping, the real grouping (no need to use \), also support the back reference;
| Or For example: A|b matches A or b,c|cat matches C or cat (matches the entire left or right).
Over.
This article is from the "stupid Bird Learn to fly" blog, please be sure to keep this source http://yshun.blog.51cto.com/9570549/1718837
Regular expressions (matches under the grep command)