Basic Introduction to Regular expressions
A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text edits, regular expressions are often used to retrieve and replace text that conforms to a pattern.
The basic elements of regular expressions include ordinary character and metacharacters characters, for example, A, B, 1, 2 characters are ordinary characters, ordinary characters can be understood literally, such as: A can only be understood as the English lowercase letter A, there is no other hidden meaning. While *, ^, [] and other meta-characters, the shell gives them beyond the literal meaning of meaning, such as: * The literal meaning of the symbol is only a symbol, but actually represents the repetition of the preceding character 0 or more hidden meanings. Therefore, mastering the basic elements of regular expressions is mainly to grasp the meaning of meta-characters in regular expressions. The POSIX standard divides regular expressions into two categories: a basic regular expression and an extended regular expression.
2.grep concept
The full name of grep is globalsearch regular expression (RE) Andprint out of the line, which translates to a comprehensive search for regular expressions and prints the lines. In fact, grep is not a separate program, but a family. Includes grep, Egrep, and Fgrep. The use of Egrep and fgrep is not significantly different from grep. Egrep is the extension of grep, an extended regular expression.
Use format for 3.grep
grep [OPTIONS] PATTERN [FILE ...]
The common [OPTIONS] are the following:
-I: Ignore case when matching
–color: Color display of matched strings
-V: Shows rows that are not matched by the pattern
-O: Displays only strings that are matched by the pattern
-a#: Indicates that the line below it is displayed after the matched row
-b#: Indicates that the line that precedes it is displayed after the matched line
-c#: Indicates that the # lines before and after the matching line are displayed
-E: Using extended regular expressions
Basic Regular Expressions:
The text regular_express.txt content used in the following example is reproduced on the bird's website. The contents are as follows.
"Open Source" is a good mechanismto develop programs.
Apple is my favorite food.
Football game isn't use feet only.
This dress doesn ' t fit me.
However, this dress was about $3183 dollars.
GNU is free air isn't free beer.
She hair is very beauty.
I can ' t finish the test.
Oh! The soup taste good.
Motorcycle is cheap than car.
This window is clear.
The symbol ' * ' is represented as start.
Oh! mygod!
The GD software is a library for draftingprograms.
You are the mean of the best is. 1.
The world <Happy> are the same with "glad".
I like dog.
Google is the best tools for search keyword.
Goooooogle yes!
Go! Go! Let ' s go.
# I AM Vbird
The colors that are displayed by the grep and Egrep commands used are aliases defined in the user's ~/.BASHRC directory
Alias grep= ' Grep–color '
Alias egrep= ' Egrep–color '
Metacharacters
.: Matches any single character
For example:
[Email protected]]# grep ' G.. G ' Regular_express.txt
Google is the best tools for search keyword.
*: Indicates a match to its preceding character any time
For example:
[[Email protected]]# grep ' goo*g ' regular_express.txt
Google is the best tools for search keyword.
Goooooogle yes!
. *: Any character of any length
For example:
[[Email protected]]# grep ' goo.*g ' regular_express.txt
"OpenSource" is a good mechanism to develop programs.
Google is thebest tools for search keyword.
Goooooogle yes!
\?: Match its preceding character 1 or 0 times
For example:
[[Email protected]]# grep ' gooo\?g ' regular_express.txt
Google is thebest tools for search keyword.
\+: The preceding character appears at least once
For example:
[[Email protected]]# grep ' goo\+ ' regular_express.txt
"OpenSource" is a good mechanism to develop programs.
Oh! The soup taste good.
Google is thebest tools for search keyword.
Goooooogle yes!
\{m\}: Exact Match of the preceding character is M times
For example:
[[Email protected]]# grep ' goo\{2\} ' regular_express.txt
goooooogleyes!
\{m,n\}: Matches the preceding character at least m times, up to N times
For example:
[[Email protected]]# grep ' goo\{2,5\} ' regular_express.txt
Goooooogle yes!
[]: Specify any single character within the matched range
For example:
[[Email protected]]# grep ' [HEF] ' regular_express.txt
Footballgame isn't use feet only.
However,this dress is about $3183 dollars.
Her hair isvery beauty.
TheWorld
[^]: Specify any single character outside of the matching range
For example:
[Email protected]]# tail-2 regular_express.txt|grep ' [^a-za-z] '
# I Amvbird
Here are some of the most commonly used special characters, not one for example
[[: Space:]]: Where [: space:] represents the white space character of this range, plus a [] to match the white space character
[[:p UNCT:]]: punctuation
[[: Lower:]]: lowercase letters
[[: Upper:]]: Uppercase
[[: Alpha:]]: Uppercase and lowercase letters
[[:d Igit:]]: Number
[[: Alnum:]]: Numbers and uppercase and lowercase letters
Anchoring for location:
^: Anchors the beginning of the line, indicating that any content behind the ^ must appear at the beginning of the line
For example:
[[Email protected]]# grep ' ^go ' regular_express.txt
Google isthe best tools for search keyword.
goooooogleyes!
Go! Go! Let ' s go.
$: Anchors the end of the line, indicating that any content in front must appear at the end of the row
^$: Indicates a blank line
For example:
[Email protected]]# grep ' ^$ ' regular_express.txt |wc-l
\< or \b: Anchor word, any character following it must appear as the first word
\> or \b: anchors the ending, any character in front of which must appear as the tail of the word
For example:
[[Email protected]]# grep ' ^\<root\> '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
[[Email protected]]# grep ' ^\broot\b '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Group:
\ (\): for example \ (ab\) * indicates that the match AB can appear any time, representing the match AB this combination, the main back reference)
For example:
[email protected]]# grep "\ (ab\) \{2,3\}"./fith.txt
Abababx
Ababababxy
Xyababaxbababy
Back reference: In the pattern, if you use \ (\) to implement a grouping, in a text check, if \ (\) pattern matches to something, the pattern behind this content can be referenced
\1: Indicates what appears in the 1th parenthesis
\2: Indicates what appears in the 2nd parenthesis
....
For example:
[Email protected]]# Cat Test
Helove his lover
Shelike her lover
Helike is Liker
Shelove her Liker
Helike him
[email protected]]# grep ' \ (L.. e\). *\1′test
Helove his lover
Helike is Liker
Here the parentheses are (L: e), \1 indicates that the front parenthesis appears to be love,\1, which represents love, parentheses
The like,\1 is the representative of the like
To extend the regular expression:
Extended regular expressions, most metacharacters are the same as basic regular expressions, and some are special as follows:
?: Do not need to use \, match its preceding characters 1 or 0 times
For example:
[Email protected]]# egrep ' goooo? ' Regular_express.txt
Goooooogle yes!
+: Its preceding character at least once, equivalent to the \{1,\ of the basic Regular expression}
For example:
[Email protected]]# egrep ' goo+ ' regular_express.txt
"OpenSource" is a good mechanism to develop programs.
Oh! The soup taste good.
Google is thebest tools for search keyword.
Goooooogle yes!
{M,n}: Do not need to use \, match its preceding characters at least m times, up to N times
For example:
[Email protected]]# egrep ' go{4,6} ' regular_express.txt
Goooooogle yes!
(): Group
For example:
[Email protected]]# egrep ' (oo) {3,5} ' Regular_express.txt
Goooooogle yes!
|: Or as a|b,a or B, is the entire left or right
AB|CD: Represents AB or CD
A (b|c) d
Fgrep: Regular expressions are not supported:
Any pattern that follows is used to match him as a character, it's faster, and if you don't need a regular expression search, you can use it
For example:
[Email protected]]# fgrep–color ' gooo ' regular_express.txt
goooooogleyes!
The above simple list of the regular expression of some of the use, to be proficient, but also need more practice
This article is from the "Devil of Tears" blog, please be sure to keep this source http://sepizi.blog.51cto.com/9773154/1596631
Introduction to the use of Linux regular expressions