Linux Regular Expression Detailed _ regular expression

Source: Internet
Author: User
Tags character set file url numeric posix egrep

A. Linux Text Lookup command

Before saying Linux regular expressions, it also describes the three common commands for finding text files in Linux:

1.grep: The earliest text-matching program that uses a POSIX-defined basic regular expression (BRE) to match the text.

2.egrep: Extended grep, which uses an extended regular expression (ERE) to match the text.

3.fgrep: Fast grep, this version matches the fixed string rather than the regular expression. And is the only version that can match multiple strings in parallel.

The following is a simple introduction to the grep command:

Syntax format:

grep [Options ...] pattern-spec [files ...]

Use:

A line of text that matches one or more patterns.

Options

-E: Use extended regular expressions to match, grep-e or replace the Egrep command.

-F: Matches with a fixed string, grep-f or replaces the traditional fgrep command.

-e: Usually the first parameter that is not an option is considered to be a pattern to match, and multiple patterns can be provided at the same time, as long as they are placed in single quotes and separated by newline characters.

When the pattern begins with a minus sign, the-e option indicates that the following argument is a pattern, even if he starts with a minus sign, to prevent confusing it with an option.

-F: Match from Pat-file file read mode.

-I: Ignores case differences when pattern matches.

-L: Lists the file names for the matching pattern instead of printing the matching rows.

-Q: Silent, if the match succeeds, does not output the matching row to the standard output, otherwise it is unsuccessful.

-S: Do not display error messages, usually with-Q.

-V: Displays rows that do not match the pattern.

Note: You can find content in multiple files at the same time, and when you specify multiple files, each displayed file line will have a file name plus a colon to identify which file it comes from.

You can use multiple-e or-f options to establish a list of patterns to find.

Two. Brief introduction of regular expressions

1. Composition of regular expressions

(1). General characters: characters without special meaning

(2). Special character (meta character): Meta character, having special meaning in regular expressions

2. The following common meta characters in regular expressions

(1). POSIX Bre and Ere are the meta characters:

\: Typically used to turn on or off the special meaning of subsequent characters, such as \ (... \) and \{...\}

. : Matches any single character (except Nul)

*: matches any number or no single character before it, for example:. Represents any character, then. * matches any length of any character

^: matches the immediately followed regular expression, Bre only at the beginning of the regular expression has a special meaning, ere in any place has a special meaning

$: Matches the previous regular expression, at the end of the string or line. The BRE has a special meaning only at the end of the regular expression, ere there is a special meaning in any position

[]: matches any character within the square brackets, where the range of consecutive characters that the hyphen (-) refers to; ^ symbol bitterness appears in the first position of the square brackets, which means that any character that is not in the list is matched.

(2). Only characters in POSIX Bre:

\{n,m\}: Interval expression that matches the number of times a single character recurs before it. \{n\} refers to reproduce n times; \{n,m\} refers to reproduce N to M times;

\ (\): Reserve space to store up to 9 separate child modes in a single pattern. such as \ (ab\). *\1: Refers to the matching AB combination of two recurrence, the middle can exist any number of characters.

\ n: Repeats the mode at this point in the \ (and \) square bracket of the nth child mode.

(3). POSIX ere only the characters:

{n,m}: same function as Bre \{n,m\}

+: One or more extensions that match the preceding regular expression

? : matches 0 or one extension of the preceding regular expression

| : matching a regular expression before or after a symbol

(): Matching the regular expression group enclosed in square brackets

(4). square brackets ([]) expressions

4.1. Character set [::]

The identity character set has the following types:

[:: Alnum]: Numeric characters [:d igit:]: Numeric characters [:p UNCT:]: Punctuation characters
[: Alpha:]: Alphabetic characters [: Graph:]: non-space characters [: Space:]: whitespace characters
[: Blank:]: spaces and positioning characters [: Lower:]: Lowercase alphabetic characters [: Upper:]: Uppercase characters
[: Cntrl:]: Control character [:p rint:]: Characters that can be displayed [: Xdigit:]: 16 in-system numbers

4.2. Sorting symbols

Refers to treat multiple characters as a symbol, such as [. Ch.] Will ch as a symbol

4.3. Equivalent character

The view that multiple characters are equal, such as [=e=] in French locale, can be matched to a variety of characters similar to E, which are no longer listed here.

Note: These three constructs must be surrounded by additional square brackets in addition to their own square brackets.

Example: [[: alpha:]!]: matches any English letter or exclamation point.

[[. Ch.]: Matches the ch sort element, not the individual letter C or H.

3. Simple formal expression matching case

China: Match rows with Chinese characters anywhere in this line

^china: Match the line with the "on" switch

china$: Matches the line ending with the

^china$: Matches only five characters in a line

[Cc]hina: Matches a line that contains either

Ch.na: Matches the line containing the CH two letter and followed by an arbitrary character followed by na two characters

Ch.*na: Matches the CH character in a row, followed by 0 or more characters, and then continues with the NA two character

Two. Examples

The following is a common example to learn bre and ere match, the source file Url.txt content is as follows:

Www.baidu.com
Http://www.baidu.com
Https://www.baidu.com
Http://wwwbaiducom
Baidu.com
Baidu

1.url match

Match begins with HTTP or HTTPS, and is followed by: and contains a. String

Bre match:

grep ' ^https\{0,1\}.*\. * ' Url.txt

Ere matched:

Grep-e ' ^https? *\.. * ' Url.txt

The results are as follows:

Http://www.baidu.com
Https://www.baidu.com

2.Email match

Sample file contents are:

Hfutwyy@qq.com
aaaa@
Aaa@.com
Aaa@gmail.com

@ @baidu. com

Matches multiple characters beginning with an alphanumeric or underscore, followed by a @ with multiple alphanumeric or underlined, with one.

grep ' ^[[:alpha:][:d igit:]_]*@[[:alpha:][:d igit:]]*\. * ' Email.txt

Match result:

Hfutwyy@qq.com
Aaa@.com
Aaa@gmail.com

Come here first, follow up and then write.

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.