Grep Regular Expression

Source: Internet
Author: User
Tags character classes grep regular expression printable characters

Grep Regular Expression

A regular expression is a symbolic representation used to recognize text patterns. In Linux, the main program for processing regular expressions is grep. Grep searches for the rows matching the Regular Expression and delivers the results to the standard output.

1. grep matching mode

Grep accepts options and parameters as follows (regex indicates a regular expression)

1
grep [options] regex [files]

The options table is as follows:

Option Description Function Description
-I Ignore case Case Insensitive
-V Invert match Unmatched
-L File-with-match Output matched file name
-L File-without-match Output unmatched file name
-C Count Number of matched output rows)
-N Number Add the file name and the number of rows in the file name before the output matching row.
-H No-filename Suppress output of file names

Example

Assume that there are three files del1, del2, and del3. The content of these files is as follows:

Example

2. special characters
Symbol Description Example
^ Start mark Examples of "^ abc" satisfying abc and abcd
^ Non () "[^ Abc]" satisfying examples: ddd, mpd
$ End mark Example abc and mmabc
. Any character Example abc, fapcc
< Match a word Examples of "<abc" satisfying abc and abcd
> Match word ends Example abc and pmrabc
| Or Example AAA, BBBpp
3. Range
Symbol Description Example
? Match the first character 0 or 1 time "Abc ?" Example AB and mabcd
* Match the first character ≥ 0 Example of "abc *" satisfying abbb and abcdk
+ Match the first character ≥ 1 Example of "abc +" satisfying abcd and abcccdd
{} {M}, {m, n}, {m,}, {, n} match the previous character m times, m to n times, ≥m times, ≤n times respectively. Examples of "abc {3, 5}" fulfillment: abcccc and abcccccc
[] [] If it is not a range, select one. If it is a range, select one. "M [abc] p" satisfies the example acpd; m [1-9] p satisfies the example m8pp
() Put all the elements of the candidate in () and separate them with | Examples of "a (1 | 2 | 3) bc" satisfying a1bc and mba3bcd

Note: {} needs to be transferred in Zheng's expression, but {} () does not.

Note:

4. Standard character classes
Character class Meaning
[: Alnum:] Letters and numbers, equivalent to [A-Za-z0-9]
[: Word:] [: Alnum _
[: Alpa:] Letter, equivalent to [A-Za-z]
[: Digit:] Number, equivalent to [0-9]
[: Xdigit:] Hexadecimal character, equivalent to [0-9A-Fa-f]
[: Blank:] Spaces and tabs
[: Graph:] Visible characters, up to 33 ~ 126
[: Lower:] Lowercase letters
[: Upper:] Uppercase letters
[: Print:] Printable characters
[: Space:] Blank characters, equivalent to [trnvf]
[: Punct:] Punctuation Marks
[: Cntrl:] ASCII control code, including 0 ~ 31, 127

Example

Linux basics tutorial ---- Regular Expression Basics

Linux Regular Expression sed details

Linux regular expression features and differences between BRE and ERE

Grep uses concise and Regular Expressions

Regular Expression usage

Assertion with Zero Width of a regular expression

Regular Expressions and file formatting commands in Linux (awk/grep/sed)

Basic Regular Expression

Regular Expressions

This article permanently updates the link address:

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.