Regular Expression basic

Source: Internet
Author: User

1. Basic Regular Expression

A. "^" matching the head of a line. "^" must be the first character in a regular expression, else it only a common character.

B. "$" matching the end of a line. "$" must be the last character in a regular expression, else it only a common character.

 

Attention: the "^" is only an anchor if it is the first character in a regular expression, else it only a common character.

The "$" is only an anchor if it is the last character in a regular expression, else it also only a common character.

If you want match a "^" in the begin of a line or "$" in the end of the line, you can use the special characters with a backslash (\).

Eg: ^ $, it can match a blank line

    

C. "." matching any silger word, cannot the end-of-line character

D. "*" repeating previous character Zero or even times.

Attention:

You must remember that Modifiers like "*", "\ {3, 5 \}", "\ {3, \} "and" \ {3 \} "only act as modifiers if they follow a common charater set.

Eg:

*: Any line contains an asterisk

\ *: Any line contains an asterisk

^ *: Any line starting with an asterisk

E. [...] specifying a range of Characters

If you want to mathc specific characters, you can use the square brackets to identify the exact characters you are searching for. The pattern that will match any line of text that contains exactly one numer is.

Attention: the characters "] 'and"-"do not have a special meaning if thed directly follow "[".

Eg:

[] The common character "[]"

[-0-9] any number or "-"

[^ 0-9] any character other than a number

[0-9-] any number or "-"

[] 0-9] any number or "]"

[0-9-z] any number, or any character between "9" and "z"

[0-9 \-A \] any number, or "-" or "A" or "]"

POSIX

[: Alnum:] alphanumeric

[: Cntrl:] Control Character

[: Lower:] lower case character

[: Space:] whitespace

[: Alpha:] alphabetic

[: Digit:] Digit

[: Upper:] upper case character

[: Blank:] whitespace, tabs, etc.

2. Extended Regular Expression

A. repeating previous character minimum and maximum times.

\ {3, 5 \}: repeating previous character 3, 4 or 5 times.

\ {3, \}: repeating previous character 3 or more times.

\ {3 \}: repeating previous character only 3 times.

Eg:

^ A \ {2, 5 \} B: Any line starting with 2, 3, 4 or 5 "a" s followed by a "B"

^ A \ {3, 5 \}: Any line starting with 3, 4 or 4 ""

^ A \ {3} \: Any line starting with 3 ""

\ {3, 5 \}: Any line contains {3, 5}

B. +, repeating the privouse character at least one times

C .? , Repeating the privouse character Zero or one times.

D. | it is meaning or between two regular expression.

Eg:

(Expressin1 | expression2 | expression3)

3 operator priority

Regular Expression operator priority is following:

\

[]

*, + ,?, \ {M \}, \ {M, \}, \ {M, N \}

Common Character

^, $

|

Regular Expression basic

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.