Basic usage of regular expressions in Linux

Source: Internet
Author: User
Tags fam

Regular Expressions: pattern-matching languages have their own syntax and rules, the most important of which are metacharacters, usually enclosed in single quotes

Use Greb to match text (Greb basic usage is to provide a regular expression and a file, but there should be a match for this regular expression in the file)

grep: (Global research print), searches for text according to a pattern, and displays lines of text that conform to the pattern.

Pattern: A matching condition formed by the combination of meta-characters of an expression wildcards regular a text word.

Greb Options

- I. uses the provided regular expression, but does not enforce case-sensitive

- v 1. Display only rows that do not contain a regular expression match (note: The matching line is displayed by default) 2. Match all lines that begin with # or; (note: #和; Indicates a typical character that the row is interpreted as a comment)

- R apply recursive matching regular expression data search to a set of files or directories

- e If you use multiple-e options, you can provide multiple regular expressions and use them in conjunction with a logical or

-A <number> shows the number of rows after a regular expression match

- b <number> shows the number of rows before a regular expression match

- o indicates that only strings that are matched to the pattern are displayed, and the entire line is displayed by default

--color displays the matched string as a highlighted color

Basic syntax for regular expressions

^ Anchor Beginning , any content behind this string must appear in the first row: grep "^l" File3

$ anchor Line end , any content preceding this character must appear in the line tail example: [[email protected] desktop]$ grep "e$" File3

^$ indicates a matching blank line example : [[email protected] desktop]$ grep "^$" File3

\< anchor Word head , any character following it must appear as the header of the word, for example: [[email protected] desktop]$ grep "\<lo" File3

Lo ver

Lo OK

Lo Okuop

\> anchor ending , any character in front of it must appear as the tail of the word, for example: [[email protected] desktop]$ grep "ily\>" File3

my fam ily is farm

We are fam ily

\<\> Exact Match example: [[email protected] desktop]$ grep "\<family\>" File3

my Family is farm

we are Family

*   Match any of the preceding characters in any instance: [[email protected] desktop]$ grep "o*" file3 (matches occurrences o any character) [[email protected] desktop]$ grep "O*t" file3 (matches the preceding O can appear any time, but must end with T) [[email protected] desktop]$ grep "oo*" File3 (indicates that the first o must match, * connected O can appear any time)

.* Any character example that represents any length : [[email protected] desktop]$ grep ". oo*" File3

  match its preceding characters once or 0 times   (a?b),  sometimes  ?  you use  \  escape to use. Example:[[email protected] desktop]$ grep  "Lo\?"  file3 (indicates l must match, O can occur one or 0 times)

\{m,n\} \ to escape and prevent {} from being parsed by bash; matches the preceding character at least m times, up to N times ;(\{1,\}: Indicates a minimum of 1 times, no upper limit). \{0,3\} represents up to 3 examples: [[email protected] desktop]$ grep "loo\{1,2\}" File3 (indicates lo must match, immediately following o at least 1 times, up to two times)

. Match any single character example: [[email protected] desktop]$ grep ". K" File3 (the following k must match, preceded by any match of one character)

Lo OK

Lo OK UOP

to OK

Lo OK Root Rool

[] matches any single character within the specified range , Example: [[email protected] desktop]$ grep "[FAMHS]" file3 (indicating that any one of the parentheses can be matched)

C a T

m y Fam ily i s farm

We a Re Fam ily

m e

[^] matches any single character outside the specified range , Example: [[email protected] desktop]$ grep "[^FOAMHS]" file3 (indicating that a single character not in parentheses can match)

\ (\) to group content, or to do a back reference , \1 represents the content that was matched in the first () call, and so on;

such as: \ (ab\) * denotes any number of AB string, AB as a whole, AB can appear 0 or more times, * modified is AB this whole

Cases:

He Love his lover

She like her liker

He Love his Liker

She like her lover

1.[[email protected] desktop]$ grep "\ (L.. e\) "TXT (indicates that the match begins with L with an e ending, any character that can match two characters in the middle)

He Love His Love R

She like Her like R

He Love His like R

She like Her Love R

2.[[email protected] desktop]$ grep "\ (L.. e\). "TXT (indicates that the match starts with L with E ending with an intermediate match of two characters and then matches any single character)

He Love His lover

She like Her Liker

He Love His Liker

She like Her lover

3.[[email protected] desktop]$ grep "\ (L.. e\). * "txt

He Love his lover

She Like her liker

He Love his liker

She Like her lover

4.[[email protected] desktop]$ grep "\ (L.. e\). *\1 "txt (means match the first parenthesis in the previous one)

He Love he Love R

She like she like R


Basic usage of regular expressions in Linux

Related Article

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.