Rookie Linux Road 1->egrep and its regular expressions

Source: Internet
Author: User

Regular Expressions (regexp:regular expression)

Regular expressions are divided into two categories:

1, the basic regular expression. Basic REGEXP

2, extended regular expression. Extened REGEXP

Basic Regular Expressions:

Character matching

. Match any single character

[]: matches any single character within the specified range

[^]: matches any single character outside the specified range

Number of Matches

* indicates that the preceding character matches any time

\? Indicates that the character before it matches 0 or 1 times

\{m,n\} indicates that the number of matches is at least m times, up to n times, and N can omit the upper limit of the number of matches

. * denotes any character that matches any length

Anchoring: Anchoring a single word

^ Anchor Beginning

$ Anchor Line End

\<,\b Anchor Word Header

\>,\b Anchor Word Tail

\ (\) used as a back reference->\1,\2,\3,...

grep command: A command that filters text using a pattern defined by a basic regular expression

Format: grep [option] Pattern file ...

Common option:

-I: Ignores the case of matched characters

-V: Reverse filtering

-O: Displays only the matched string, not the entire row

--color: Displays the color of the matched text

-E: Using extended regular expressions

-A (after) #: When a row is matched by the grep command, not only does it display the contents of the line, but the line after the line is also displayed

-B (before) #: When a row is matched by the grep command, not only does it display the contents of the line, but the line before the line is displayed.

-C (Context: Contexts) #: When a row is matched by the grep command, not only does it show the contents of the line, but the line before and after the line is displayed

By default, regular expressions work in greedy mode


Extended Regular Expression

Character matching

. Match any single character

[] matches any single character within the specified range

[^] matches any single character outside the specified range

Number of Matches

* Match its previous characters any time

? Match its previous characters 0 or 1 times

+ Match its previous characters at least once = \{1,\}

Example: grep--color-e ' [[: space:]]+ ' ... Represents a line that filters out text, starting with at least one white space character

{M,n} indicates that the number of matches is at least m, up to N, and the curly braces for the extended regular expression do not need a backslash

Position anchoring

^ Anchor Beginning

$ Anchor Line End

\< Anchor Word Header

>\ Anchor Word Tail

Group

(): Group->\1,\2,\3,...

or | is a special character in an extended regular expression

|:o R

Example:

C|cat represents C or cat

If you want to achieve cat or cat effects, use the grouping

' (c|c) at ' can be implemented

Egrep = Grep-e Extended Regular expression

Example: Take out 1-255 of the integers in the file

Egrep--color ' \b ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \b ' ...

\. \ is a translator, the function of the translator is to let the metacharacters represent its own meaning, rather than the meaning of metacharacters, it represents a point

Egrep--color ' (\b ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \b) {#} ... '

Indicates that the contents of the preceding () repeats the # times in {}


IP Address:

Ipv4->

There are five types of addresses, such as Class A, Class B, Class C, Class D, E, for us only ABC three, the remaining two classes are used for research use

Class A: First paragraph of IP from 1-127

Class B: First paragraph of IP from 127-191

Class C: The first segment of IP from 192-223


There are three grep commands, except grep, Egrep, and a fgrep command

grep only supports commands for basic regular expressions by default

Egrep is a command that supports extended regular expressions

Fgrep = Fast grep quick grep

grep does a pattern match to search for text, which wastes a lot of CPU clock cycles, and when you use Fgrep, any character in the pattern will be matched as the character itself, Fgrep does not support regular expressions, so it does not search for regular expressions, so it is fast





This article is from the "Novice Technical Documents" blog, please be sure to keep this source http://zhubo.blog.51cto.com/11395641/1827128

Rookie Linux Road 1->egrep and its regular expressions

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.