Regular Expression format and usage

Source: Internet
Author: User

Use regular expressions in various formats and usage

Commands that use regular expressions for search and search are commonly used: grep (search), egrep (Extended grep), and fgrep.

Grep: Use regular expressions, text information, and other conditions to retrieve information in a file and display lines containing matching strings.

Format: grep [Options] 'pattern' filename

Options: Option (omitted)

Pattern: Regular Expression and text

Filename: File Name

Option:

-I: case-insensitive characters

-- Color: color the matched string.

-O: only matching strings are displayed"

-V: displays unmatched rows.

-A #: displays the matched rows and the following # Rows (total # + 1 rows)

-B #: display the matched rows and the above # Rows

-C #: display the matched rows and the above # Rows and above # Rows (total # + 1 rows)

Pattern:

'.': Indicates a single arbitrary character.

'*': Indicates matching any character before

'. *': Any character of any length

'[]': A line that matches any single character

[: Alpha:] [: digit:] [: alnum:] [: lower:] [: Upper:]

'[^]': Match any row other than this character

\ {M, N \}: match the first character at least m times, at most N times

\? : Match the first character 0 or 1 time

\ <: Match the line with the first character after the word \>: match the line with the last character before the word

\ <String \>: match the row where the word 'string' is located

^: Match the row whose first line is its second character $: match the row whose last line is its first character

^ $: Matches blank rows

^ String $: the row that matches the entire 'string'

\ (String \): Group

Example: ^ \ (ABC \) [[: Space:] \ 1 $: match the line with the content 'abc abc'

\ 1 indicates the first group in the expression, \ 2: indicates the second group, and so on




Extension expression

Format: egrep [Options] pattern [files...]

{M, n} matches the first character at least m times and at most N times

+ Match the first character at least once. Similar to {1 ,}

() Group

| Or B | boy indicates B or boy (B | boy) indicates boy or boy

Other usage is similar to grep





Fgrep does not support regular expressions.

This article is from the "xpzz51" blog. For more information, contact the author!

Regular Expression format and usage

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.