The regular expression of Linux learning &grep&egrep

Source: Internet
Author: User
Tags character classes expression engine egrep

We often need to search the documentation for content that meets our requirements, which may be scattered around the document in every corner. Can use keywords such as/keyword or? Keyword one search, and I might not just want to search for keywords, but to specify a range, how to do? And how do you show the contents of these searches in a centralized way? The grep command and the Egrep command, which uses regular expressions to search for strings, can satisfy this requirement.

Regular Expressions (Regular expression) is a pattern of character writing, the processing of characters in the unit of behavior, through the assistance of some special characters, using this mode can easily reach the search, deletion, substitution of characters. The special characters mentioned above are called Meta characters.

A regular expression is a notation that, as long as the related commands for string processing support this notation, it is possible to invoke the meta character of the regular expression to handle the string.

Regular expressions are divided into basic regular expressions and extended regular expressions.

The meta-characters used in the basic regular Expressions have the following categories:

    1. Character Matching classes

Metacharacters Representative meaning
. Any single character
[] Any single character within the specified range
[^] Any single character in a non-specified range

2. Character classes

Metacharacters Representative meaning
[[:d Igit:]] Number, 0-9
[[: Lower:]] Small Letter, A-Z
[[: Upper:]] Capital letters, A-Z
[[: Alpha:]] Any size letter, a-z,a-z
[[: Alnum:]] Uppercase and lowercase letters and numbers, a-z,a-z,0-9
[[: Space:]] Blank characters of all types, such as SPACEBAR, TAB key, enter, etc.
[[:p UNCT:]] Punctuation
[[: Blank:]] Space bar and TAB key generated blanks


3. Number of times Match class: Specifies the number of occurrences of a character before a metacharacters

Metacharacters Representative meaning
*
Any time, including 0 times
\? 0 or 1 times
\{m\} Accurate m-time occurrence
\{m,n\} At least m times, up to N times
\{m,\} At least m times
\{0,n\} Up to N times
.* Any character of any length

4. Position anchoring: Specifies where the string appears in the row

"/ strong> means
^
$
^$
\< Word first
\> word ending
\b used for the beginning of the word with \< Used for ending with \>

5. Grouping: Referencing a set of strings as a single character

The patterns in the grouping match to the content that can be remembered in memory by the regular expression engine and then referenced. The content enclosed in parentheses is understood by the regular expression as a grouping, \ (\). such as \ (abc\). These () are numbered and have multiple parentheses in the case, how to determine which pair is a group? And which group to refer to? such as \ (Abc\ (def\) g\). *\ (lmn\), there are three groups (), there are three groupings, "Def" is a grouping, "ABC (DEF) g" is a grouping (including small group "Def"), and a "ABC (DEF) g" Separate grouping "LMN", 650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/57/73/wKiom1SaYbuB79qAAABPGJ8n19s819.jpg "title=" group. png "alt = "Wkiom1saybub79qaaabpgj8n19s819.jpg"/> from left to right, the red Arrow points to the first group, the purple points to the second group, and the right half of the corresponding color in the back.

"\ (abc\ (def\) g\). *\ (lmn\) \1" followed by "\1" means to refer to the 1th left parenthesis from the left and the closing parenthesis that corresponds to it, namely "abc\ (def\) g". "\2" is referring to "def". So "\#" is referring to the contents of the parentheses from the left.

Example: "\ (ab\{1,3\}c\). *\1", with "ABBCDDEABBC"; " ABBBCEFBB ";" ABBBBCXXZABBBBC ";" Abbcssab "; Which of these strings meets our re format requirements? One rule of judgement is that the part of the "\#" referred to in the back and the preceding must be exactly the same, so it's good to judge.


Second, the meta-character used to extend the regular expression

1. The character matching class is the same as the basic re

2. The character class is the same as the basic re

3. "*", "?", "{m,n}" are escaped without the escape character "\", and a "+" is added to indicate that the preceding character is repeated at least once, with the same meaning as {1,}.

4. Position anchoring with basic re

5. The grouping "()" brackets do not have to be escaped with the escape character "\".

6, or "|" "A|b" means "a" or "B", and multiple characters such as "abc|def" means "abc" or "Def" instead of "ABCEF" or "abdef". So "|" With the () Mate, you can select a string range.


Three, grep, Egrep

grep (globally search a regular expression and print) usage:

grep [Options] PATTERN [FILE ...]

Common options:

-V: Displays the line of a string that does not include the regular expression

-O: Show only the matched string, not the line where the string is located

-i:ignore-case, ignoring character case

-E: Supports the use of extended regular expressions

-A #

-B #

-C #

Egrep (enhanced grep), equivalent to GREP-E


Common options:

Iv. examples

Title: Write a pattern that can match a reasonable IPv4 address.

Resolution process: I set the environment to extract a reasonable IPV4 address from the command "Ifconfig". Egrep processing with support for extending the RE command.

This question for my knowledge level is still a bit difficult, the middle wrote a lot, there are a variety of different matching results. Some of the results can only show a single number, some of the thousands of numbers are also screened out, the final success to achieve the goal of the wording is:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/74/wKiom1SadAjQ9fyUAAHRrhabFL8686.jpg "title=" Filter the IPV4 address. png "alt=" wkiom1sadajq9fyuaahrrhabfl8686.jpg "/>

Use the option "-O" to display only the string itself that meets the requirements, and the result is

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/74/wKiom1SadKaSt93sAAGAEZwSpTQ736.jpg "title=" Ip4.png "alt=" Wkiom1sadkast93saagaezwsptq736.jpg "/>


Look at the output of the ifconfig:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/76/wKiom1SaflGSPnU9AAM0r9fvytc908.jpg "title=" Ifconfig.png "alt=" Wkiom1saflgspnu9aam0r9fvytc908.jpg "/>

Of course there are other more concise re-writing, as a math from a bad person to write so long a string of numbers, instantly feel oneself tall on the 650) this.width=650; "Src=" http://img.baidu.com/hi/jx2/j_0063. GIF "alt=" j_0063.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0067.gif "alt=" J_0067.gif "/>

This article is from the "Studylinux" blog, make sure to keep this source http://studylinux.blog.51cto.com/2450175/1595597

The regular expression of Linux learning &grep&egrep

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.