Linux Regular expression syntax

Source: Internet
Author: User
Tags alphabetic character posix

Basic Components :

The basic component of a regular expression.

Regular expressions

Describe

Example

\

Escape characters, escaping special characters, ignoring their special meanings

A\.B matches a.b, but does not match AJB,. Escaped as a special meaning

^

Matches the beginning of the line, awk, ^ is the start of the matching string

^tux matches lines starting with Tux

$

Matches the end of the line, in Awk, $ is the ending of the matching string

tux$ matches lines ending with Tux

.

Matches any single character except the line break \ n, awk can

AB. Matches an ABC or bad, does not match ABCD or Abde, only matches single character

[ ]

Match any one of the characters contained in [character]

COO[KL] can match cook or cool

[^ ]

Match any character other than [^ character]

123[^45] can not match 1234 or 1235,1236, 1237 can

[ - ]

Matches any character in the specified range of [], to be written as an increment

[0-9] can match 1, 2 or 3 of any one of the numbers

?

Match previous items 1 or 0 times

Colou?r can match color or colour, and cannot match Colouur

+

Match the previous item 1 or more times

sa-6+ matches sa-6, sa-666, cannot match sa-

*

Match the previous item 0 or more times

Co*l matches cl, col, cool, coool, etc.

()

Match expression to create a substring to match

MA (tri)? Match Max or Maxtrix

N

Matches the previous item n times, n is a positive integer that can be 0

[0-9] {3} matches any three-digit number and can be extended to [0-9][0-9][0-9]

{N,}

The previous item must match at least n times

[0-9] {2,} matches any two-digit or more number of digits

{N,m}

Specifies that the preceding item matches at least N times, up to M times, n<=m

[0-9] {2,5} matches any number from a two-digit to a five-digit number

|

Alternate Match | Any one of two sides

AB (C|D) matches ABC or ABD

POSIX character class

The POSIX character class is a special meta-sequence (meta sequence) of a shape such as [: ...:] that can be used to match a specific range of characters.

Regular expressions

Describe

Example

[: Alnum:]

Match any one letter or number character

[[: alnum:]]+

[: Alpha:]

Match any alphabetic character (including uppercase and lowercase letters)

[[: Alpha:]] {4}

[: Blank:]

Spaces and tabs (Landscape and portrait)

[[: blank:]]*

[:d Igit:]

Match any numeric character

[[:d Igit:]]?

[: Lower:]

Match lowercase letters

[[: Lower:]] {5,}

[: Upper:]

Match uppercase letters

([[: upper:]]+)?

[:p UNCT:]

Match punctuation

[[:p UNCT:]]

[: Space:]

Matches all whitespace characters including line feed, carriage return, and so on

[[: space:]]+

[: Graph:]

Matches any character that can be seen and can be printed

[[: Graph:]]

[: Xdigit:]

Any one hexadecimal number (i.e.: 0-9,a-f,a-f)

[[: xdigit:]]+

[: Cntrl:]

Any one control character (the first 32 characters in the ASCII character set)

[[: Cntrl:]]

[:p rint:]

Any one of the characters that can be printed

[[:p rint:]]

Metacharacters

Metacharacters (meta character) is a Perl-style regular expression that is supported by only a subset of text processing tools, not all text processing tools.

Regular expressions

Describe

Example

\b

Word boundaries

\bcool\b matching cool, mismatched coolant

\b

Non-word boundary

cool\b matching coolant, not matching cool

\d

Single numeric character

B\DB Match Business-to-business, mismatch BCB

\d

Single non-numeric characters

B\db matching BCB, mismatch business-to- business

\w

Single word characters (letters, numbers, and _)

\w match 1 or A, mismatch &

\w

Single non-word character

\w match &, mismatch 1 or a

\ n

Line break

\ n matches a new row

\s

Single whitespace character

X\SX matches x x, does not match xx

\s

Single non-whitespace character

X\s\x matches Xkx, does not match xx

\ r

Enter

\ r Match carriage return

\ t

Horizontal tab

\ t matches a horizontal tab

\v

Vertical tab

\v matches a vertical tab

\f

Page break

\f match a page break

Linux Regular expression syntax

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.