The use of common regexp in Linux environment

Source: Internet
Author: User

Regular expressions

Shorthand for REGular EXPression
Metacharacters

Number of Matches

Position anchoring

Group

--------------------------------------
Metacharacters
. Match any single character

[] matches any single character in the specified range


[^] matches an outside of the specified range
[: Space:] white space characters
[:p UNCT:] punctuation character
[: Lower:] lowercase letters
[: Upper:] Uppercase
[: Alpha:] uppercase and lowercase letters
[:d Igit:] Number
[: Alnum:] numbers and uppercase and lowercase letters


Number of matches (greedy mode)

* Match any of its preceding characters any time
A*b B Front A appears any number of times
A.*b A begins with B ends
. * Any character of any length

\? Match its preceding character 1 or 0 times meaning that this character is dispensable
A?b
\{m,n\} matches characters preceding it at least m times up to N times
\{1,\} at least once
\{,3\} up to 3 times
A.\{1,3\}b A and B have at least one of three characters

Location anchoring :

^ Anchor Line Any content after this character must appear at the beginning of the line

$ anchor Line end any content that precedes this character must appear at the end of the row
grep ' B. h$ '/etc/passwd
^$ Blank Line
\< any character that follows it must appear as a word header
\> any character in front of it must appear as the tail of the word \b Another way of writing this symbol


Group
\(\)
\ (ab\) * AB can appear any time
\1 the first opening parenthesis and the corresponding group contents

grep ' \ (L.. e\). *\1 ' Test3.txt


Exercise: Analyze the characteristics of the first two lines in the following text in the/etc/inittab file (the number must be the same in each row), write a pattern that can be found exactly like two lines
11:1:WAIT:/ETC/RC.D/RC 1

13:1:WAIT:/ETC/RC.D/RC 3

grep ' ^1\ ([0-9]\): \1.*\1$ '/etc/inittab

grep commands to filter text using patterns defined by the basic regular expression


----------------------------------------------------
Extending regular Expressions

Character matching
. [] [^]

Number of Matches
*
? No backslashes.
+ match the characters in front of it at least once \{1,\}
{M,n} does not require backslashes

Position anchoring
^
$
\<
\>

Group
() No backslash
\1,\2

Or
The meaning of a|b or
C|cat the whole left and right.
Cat or cat Error
C or Cat Pair

Grep-e ' C|cat ' text.txx

{3} repeated three times

Fgrep does not support regular expressions particularly fast

The use of common regexp in Linux environment

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.