14. Study progress and task of self-study--linux "Regular expression"

Source: Internet
Author: User
Tags egrep

Regular expressions

grep, Egrep, and fgrep of text processing tools

1, the basic regular expression

      grep: (Global search Regular expression (RE) and print out of the line)
Text Search tool to search the target file on a row-by-line basis based on user-specified text patterns and display matching rows

        # grep [options] ' Patterf (mode) ' file, default only supports basic regular expressions, characters to match extra features with-E
--color=auto Specifying Colors
-V: Reverse match, showing rows that cannot be matched by the pattern
-O: Matches only strings that are matched to the pattern, not the entire row
-I: Case insensitive, ignore-case
-E: Support extended
-A #: also displays the following line in the mode
-B #: Also displays a row above the pattern
-C #: Display one row before and after each


Regular expression: is the pattern (pattern) written by a class of characters
Metacharacters: Similar to wildcard characters, does not represent the meaning of the character itself, for the description of additional functionality

The metacharacters of the basic regular expression:


character match:
.: Any single character
[]: any single character within the specified range                 
[0-9],[[:d Igit:]]
[A-z],[[:lower:]]
[A-z],[[:upper:]]
all letters: [[: Alpha:]]
letter + Number: [[: alnum:]]
space: [[: Space:]]
Punctuation: [[:p UTCT:]]
[^]: Specify any single character outside the range



Number of occurrences: used to specify the number of occurrences of the preceding character
*: Any time
Example: X*y xxy xy y can match
. *: Matches any character of any length
\?: The preceding character appears 0 or 1 times
\{m\}: Matches the preceding character m times
\{m,n\}: At least m times, up to N times
\{m,\}: At least m times
\{0,n}: Up to n times
Use "" To do variable conversions


Greedy mode: to match characters as long as possible:

Position anchoring: Used to specify where characters appear
^: Anchoring the beginning of the line
^char
$: Anchor Line End
grepchar$
^$: Blank Line



The position of the word is anchored:

\<char: Anchor word head, \bchar
Char\>: Anchor ending, char\b



Group:
\(\)
\ (ab\) *xy



Reference:
\1: A back reference, referring to the preceding first opening parenthesis and the corresponding pattern in the right parenthesis to match the content, meaning that the previous occurrence, in \1 this also appears once
\2
...
\ (A.B) xy\1:a6bxya6b

2. Extended Regular Expressions

    Egrep: Use an extended regular expression to build the pattern, which is equivalent to GREP-E
Metacharacters
Character Matching:
.: Any single character
[]: Any single character within the specified range
[^]: Any single character outside the specified range

Number of matches:
*: The character in front of the match is any time
? : The character in front of the match 0 or 1 times
+: Match its preceding character at least once
{m}: matches its preceding character m times
{M,n}: At least m times, up to N times
{m,}: at least m times
{0,n}: up to n times


Position anchoring: Used to specify where characters appear
^: Anchoring the beginning of the line
^char
$: Anchor Line End
grepchar$
^$: Blank Line


The position of the word is anchored:
\<char: Anchor word head, \bchar
Char\>: Anchor ending, char\b


Group:
(): Group
|: Or, AC|BC ac or BC

  Fgrep: Non-parsing of regular expressions

14. Study progress and task of self-study--linux "Regular expression"

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.