Regular expression Usage

Source: Internet
Author: User
Tags expression engine egrep


Regular Expressions: Regular expression, REGEX

Divided into two categories:

Basic Regular Expressions: BRE

Extended Regular expression: ERE

grep family:

Grep:global search REgular expression and Print out of the line. Supports the use of basic regular expressions;

Egrep: Supports the use of extended regular expressions;

Fgrep: The use of regular expressions is not supported;

grep command:

Function: Text Search tool, according to the user specified "pattern (filter)" to match the target text line by row to check, print out the qualifying line;

Pattern: The filter condition written by the text character and the regular expression meta-character;

Common options:

--color=auto: Color The matching text to highlight;

-I: ignore character case;

-O: Displays only the text that matches to itself;

-V,--invert-match: reverse match;

-E: Support for extended regular expressions;

-Q,--quiet,--silient: Silent mode, do not output any information;

Basic regular Expression meta-characters:

Character Matching:

.: matches any single character;

[]: matches any single character within the range;

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

[:d Igit:],[:lower:], [: Upper:], [: Alpha:], [: Alnum:], [: Space:], [: Blank:], [:p UNCT:]

Mans 7 Glob

Number of matches:

Used to specify the number of occurrences of the character to be preceded by a limit to how many times the preceding characters appear, the default work in greedy mode;

*: matches the preceding character any time (0,1 or more);

grep "X*y":

Xxxyabc

Yabc

Abcxy

Abcy

. *: Any character of any length;

\+: Matches the preceding character at least 1 times;

grep "X\+y":

Xxxyabc

Yabc

Abcxy

Abcy

\?: matches the preceding 0 or 1 times, that is, the preceding character is optional;

grep "X\?y":

Xxxyabc

Yabc

Abcxy

Abcy

\{m\}: The preceding character appears m times and M is a nonnegative integer;

grep "X\{2\}y":

Xxxyabc

Yabc

Abcxy

Abcy

\{m,n\}: The preceding character appears m times and M is a nonnegative integer; [M,n]

\{0,n\}: Up to n times;

\{m,\}: at least m times;

Position anchoring

Restricts the use of pattern search text, which restricts the text that the pattern matches to where it appears only in the target text;

^: Anchor at the beginning of the line; for the leftmost side of the pattern, ^pattern

$: End of line anchoring; for the right side of the pattern, pattern$

^pattern$: To make PATTERN exactly match a whole line;

^$: Empty line;

^[[:space:]]*$:

Word: A continuous character consisting of non-special characters (a string) is called a word;


\< or \b: The initial anchor for the left side of the word pattern, formatted as \<pattern, \bpattern

\> or \b: The ending anchor for the right side of the word pattern, formatted as PATTERN\> pattern\b

\<pattern\>: Word anchoring;

Instance:

1. Display the line in the/etc/passwd file that does not end with bash;

~]# grep-v "bash$"/etc/passwd

2. Find out the three-bit or four-digit number in the/etc/passwd file;

~]# grep "\<[0-9]\{3,4\}\>"/etc/passwd

3. Find the/etc/grub2.cfg file, start with at least one whitespace character, followed by a line with non-whitespace characters;

~]# grep "^[[:space:]]\+[^[:space:]"/etc/grub2.cfg

4. To find the result of "Netstat-tan" command, the line ending with ' LISTEN ' followed by 0 or more whitespace characters;

~]# Netstat-tan | grep "listen[[:space:]]*$"

5. Find the result of "fdisk-l" command, including the line with/dev/followed by SD or HD and a small letter;

~]# Fdisk-l | grep "/dev/[sh]d[a-z]\>"

6. Find the file path in the result of "ldd/usr/bin/cat" command;

~]# Ldd/usr/bin/cat | Grep-o "/[^[:space:]]\+"

Grouping and referencing:

\ (pattern\): The character matching this PATTERN is treated as a non-infringing whole;

Note: The patterns in the grouping brackets match the characters that are automatically recorded in the internal variables by the regular expression engine, which are \1, \2, \3, ...

Pat1\ (pat2\) pat3\ (pat4\ (pat5\) pat6\)

\ n: The string that matches the pattern between the nth opening parenthesis in the pattern and the closing parenthesis that matches it (not the pattern, but the result of the pattern match)

\1: The string that the pattern in the first set of parentheses matches to;

\2: The string that the pattern in the second set of parentheses matches to;

......

He love his lover.

He like his lover.

He love his liker.

He like his liker.

L.. E.*l. Er

\ (L.. e\). *\1r

Back reference: Refers to the string that matches the pattern in the preceding parentheses;

Two common options:

-E,--extended-regexp: supports the use of extended regular expressions

-F,--fixed-strings: Supports the use of fixed strings, does not support regular expressions, and is equivalent to Fgrep;

-G,--basic-regexp: Supports the use of basic regular expressions;

-P,--perl-regexp: supports the use of pcre regular expressions;

-E pattern,--regexp=pattern: multi-mode mechanism;

-F file,--file=file:file a text file containing a pattern for each line, the grep script;

-A NUM,--after-context=num

-B NUM,--before-context=num

-C NUM,-num,--context=num

Egrep

The grep command that supports the use of extended regular expressions is equivalent to GREP-E;

Egrep [OPTIONS] PATTERN [FILE ...]

Extend the metacharacters of regular expressions:

Character Matching:

.: Any single character

[]: Any single character within the range

[^]: Any single character outside the range

Number of matches:

*: any time;

?: 0 or 1 times;

+:1 or multiple times;

{m}: matches m times;

{M,n}: At least m times, up to n times;

{0,n}

{m,}

Location anchoring:

^: Beginning of the line

$: End of line

\<, \b: The head of the word

\>, \b: suffix

Grouping and referencing:

(pattern): a grouping in which the pattern in parentheses matches to a character that is recorded in a variable inside the hermetical expression engine;

Back reference: \1, \2, ...

Or:

A|b:a or B

C|cat: Indicates C or cat

(C|C) at: Indicates Cat or cat

Instance:

1. Display the line in the/etc/passwd file that does not end with bash;

~]# egrep-v "bash$"/etc/passwd

2. Find out the three-bit or four-digit number in the/etc/passwd file;

~]# egrep "\<[0-9]{3,4}\>"/etc/passwd

3. Find the/etc/grub2.cfg file, start with at least one whitespace character, followed by a line with non-whitespace characters;

4. To find the result of "Netstat-tan" command, the line ending with ' LISTEN ' followed by 0 or more whitespace characters;

5. Find the result of "fdisk-l" command, including the line with/dev/followed by SD or HD and a small letter;

6. Find the file path in the result of "ldd/usr/bin/cat" command;

7. Find all the lines in the/proc/meminfo file that begin with uppercase or lowercase s; at least three ways;

~]# egrep "^ (s| S) "/tmp/meminfo

~]# grep "^[ss]"/tmp/meminfo

~]# grep-i "^s"/tmp/meminfo

8. Displays information about root, CentOS, or Slackware users on the current system;

~]# egrep "^ (root|centos|slackware) \>"/etc/passwd

9, echo output an absolute path, using Egrep to remove its base name;

~]# echo/etc/passwd/| Egrep-o "[^/]+/?$"

10. Find the integer between 1-255 in the result of ifconfig command;

~]# Ifconfig | Egrep "\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "

11, add user Bash, Testbash, basher and Nologin, the default shell for the first three users is/bin/bash,nologin default shell is/sbin/nologin, Then find the user whose username is the same as the shell name;

~]# egrep "^ ([[a-z0-9]+) \>.*\1$"/etc/passwd


This article from the "Simple Linux" blog, reproduced please contact the author!

Regular expression 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.