Wildcard wildcards regular expressions in Linux

Source: Internet
Author: User
Tags expression engine egrep

Wildcard characters:

File name wildcard is one of the features of bash, and wildcards are typically used for file name matching.

*: Any character of any length

? : Matches any single character

[]: matches any single character in the specified range

Example: [ABC], [A-z], [0-9], [0-9a-z]

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

Example: [^0-9a-z]

Character Set:

[: Space:]: all whitespace characters

[:p UNCT:]: All punctuation

[: Lower:]: All lowercase letters

[: Upper:]: All uppercase letters

[:d Igit:]: All numbers

[: Alnum:]: All numbers, letters

[: Alpha:]: All letters

Linux Text Processing tools:

grep, Egrep, fgrep (for file content matching, converting filenames to character channeling can also be searched with these tools)

The text Search tool, which searches the target file based on a user-specified text pattern (search criteria), and displays the rows that can be matched.

Regular Expressions:

Regular Expressions are patterns written by a class of characters, many of which do not represent their literal meanings, but rather functions such as expression control or wildcard, which are commonly used for file content matching (wildcard constants are used for matching filenames), and to convert filenames to character channeling. Regular expressions can be divided into basic regular expressions and extended regular expressions.

1) Basic Regular expression: grep

2) Extended Regular expression: Egrep, grep-e

Fgrep:fast, using regular expressions (find matching content directly) is not supported

1) Metacharacters of the basic regular expression:

Character Matching:

. : Matches any single character (as distinct from a wildcard)

[]: matches any single character within the specified range

[0-9], [[:d Igit:]]

[A-z], [[: Lower:]]

[A-z], [[: Upper:]]

[[: Space:]]

[[:p UNCT:]]

[[: Alpha:]]

[[: Alnum:]]

[^]: take the reverse. Example: [^[:alpha:]], matching all non-letter

Number of matches metacharacters: the number of occurrences of the character that is specified before it

*: Any length, the characters in front of it can appear any time

Example: X*y

\?: 0 or 1 times, the characters in front of it are optional

Example: X\?y

\{m\}: M times, it before the character to appear m times

Example: X\{2\}y

\{m,n\}: At least m times, up to N times

Example: X\{2,5\}y

\{m,\}: At least m times

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

. *: Any character of any length ( work in greedy mode: try to match as much as possible )

Location anchoring:

^: Anchor at the beginning of the line;

Write on the left side of the pattern

$: End of line anchoring;

Write on the right side of the pattern

^$: Blank Line

Matches a word (a string consisting of consecutive characters that do not contain special characters ):

\<: The first word appears on the left side of the word, \b

\<char (or \bchar)

\>: Ending, appearing on the right side of the word, \b

Char\>(or char\b)

Group:

\(\)

For example: \ (ab\) *

The contents of a pattern in a grouping, which can be remembered in memory by the regular expression engine, can then be referenced

Reference:

\#: Refers to the content to which the nth parenthesis matches, not the pattern itself

For example:

\ (ab\?c\). *\1 parentheses match to the content is ABC, so the back \ 1 matches only ABC

Command Options (grep):

-V: Reverse Selection

-O: Displays only the matched string, not the line where the string is located

-i:ignore-case, ignoring character case

-E: Supports the use of extended regular expressions

-A # shows the first few lines

-B # shows the following lines

-C # show Upper and Lower lines

2) Extended Regular expression (no escape characters required):

Character Matching:

.

[]

[^]

Number of matches:

*: Any time

? : 0 or 1 times

+: at least 1 times; (one more than the basic regular)

{m}: exact match m times

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

{m,}: at least m times

{0,n}: up to n times

Anchoring:

^: Anchor at the beginning of the line;

$: End of line anchoring;

\<, \b (You need to escape here)

\>, \b (ibid.)

^$, ^[[:space:]]*$

Group:

()

References: \1, \2, \3 (with basic regular expressions)

Attention:

A|b:a or B

Conc|cat:conc or Cat

con (c|c) at: concat or Concat

To extend the use of regular expressions:

Grep-e ' PATTERN ' FILE ...

Egrep ' PATTERN ' FILE ...


Wildcard wildcards regular expressions in Linux

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.