Common Regular Expression Syntax examples

Source: Internet
Author: User
Tags alphabetic character character set control characters expression integer numeric range string
Grammar | Regular regular Expressions grammar sentences
Here are some examples of regular expressions that you might encounter:


/^\[\t]*$/"^\[\t]*$" matches a blank line.

/\d{2}-\d{5}/"\d{2}-\d{5}" verifies that an ID number is made up of a 2-bit word, a
A hyphen and a 5-digit number.

/< (. *) >.*<\/\1>/"< (. *) >.*<\/\1>" matches an HTML tag.


The following table is a complete list of metacharacters and its behavior in the context of regular expressions:

Character description

\ marks the next character as a special character, or a literal character, or a later
To a reference, or to a octal escape character. For example, ' n ' matches the character ' n '. ' \ n '
Matches a line feed character. Sequence ' \ ' matches ' \ ' and ' \ (' Matches ' (".

^ matches the start position of the input string. If you set the RegExp object's
The Multiline property, ^ also matches the position after ' \ n ' or ' \ R '.

$ matches the end position of the input string. If you set the RegExp object's
The Multiline property, $ also matches the position before ' \ n ' or ' \ R '.

* Match the preceding subexpression 0 or more times. For example, zo* can match "Z" and
"Zoo". * is equivalent to {0,}.

+ matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" to
and "Zoo", but cannot match "Z". + is equivalent to {1,}.

? Match the preceding subexpression 0 times or once. For example, "Do (es)" can match
"Do" in "do" or "does". is equivalent to {0,1}.

{n} n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match
' O ' in ' Bob ', but can match two o in ' food '.

{N,} n is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match
' O ' in ' Bob ', but can match all o in ' Foooood '. ' O{1,} '
Equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.

{n,m} m and n are non-negative integers, where n <= m. At least match n times and most horses
with M times. Liu, "o{1,3}" will match the first three o in "Fooooood".
' o{0,1} ' is equivalent to ' o '. Note that you cannot have spaces between commas and two numbers

? When the character is immediately followed by any other qualifier (*, +,?, {n}, {n,},
{n,m}) After that, the matching pattern is not greedy. Non-greedy mode as little as possible
Matches the searched string, and the default greedy pattern matches as many as possible.
The string of the rope. For example, for the string "oooo", ' o+? ' will match a single
"O", and ' o+ ' will match all ' o '.

. Matches any single character except "\ n". To match any of the ' \ n '
He Gofu, please use a pattern like ' [. \ n] '.

(pattern) matches the pattern and gets the match. The resulting match can be obtained from the
Matches collection, using the Submatches collection in VBScript,
The $0...$9 property is used in Visual Basic scripting Edition. To
Match the parentheses character, use ' \ (' or ' \ ').

(?:p Attern) matches pattern but does not get a matching result, which means it is a non-fetching horse
and is not stored for later use. This is combined with the "or" character (|)
The various parts of a pattern are useful. For example, ' Industr (?: y|ies)
is a more abbreviated expression than ' industry|industries '.

(? =pattern) forward lookup, matching search characters at the beginning of any string matching pattern
String. This is a non-fetch match, which means that the match does not need to be fetched
Use. For example, ' Windows (? =95|98| nt|2000) ' Can match ' Windows
2000 "Windows" in, but cannot match "windows" in "Windows3 1".
Pre-check does not consume characters, that is, after a match takes place at the last time the horse
After match immediately start the next matching search, not from the word containing the pre-check Fu Zhi
Start After.

(?! pattern) Negative pre-check, in any mismatch negative lookahead matches the
Search string at no point where a string is not matching
The string at the beginning of pattern matches the lookup string. This is a non-fetching horse.
In other words, the match does not need to be acquired for later use. For example, ' Windows
(?! 95|98| nt|2000) ' Can match windows in Windows 3.1,
However, you cannot match windows in Windows 2000. Pre-check does not consume words
character, that is, after a match occurs, immediately after the last match
Start the next matching search instead of starting after the character that contains the pre-check

X|y matches x or Y. For example, ' Z|food ' can match "z" or "food". ' (z|f)
Ood ' matches ' zood ' or ' food '.

[XYZ] Character set combination. Matches any one of the characters contained. For example, ' [ABC] ' Can
Match ' a ' in ' plain '.

[^XYZ] Negative character set combination. Matches any characters that are not included. For example, ' [^ABC] ' can
Match the ' P ' in ' plain '.

[A-z] character range. Matches any character within the specified range. For example, ' [A-z] ' can be
Any lowercase alphabetic character in the range of ' a ' to ' Z '.

[^a-z] a negative character range. Matches any character that is not in the specified range. For example
' [^a-z] ' can match any character that is not in the range ' a ' to ' Z '.

\b Matches a word boundary, which refers to the position between the word and the space. For example
' er\b ' can match ' er ' in ' never ', but cannot match ' verb '
' ER '.

\b Matches a non word boundary. ' er\b ' can match ' er ' in ' verb ', but not a
With "Never" in the ' er '.

\CX matches the control characters indicated by X. For example, \cm matches a control-m or
The carriage return character. The value of x must be one-a-Z or a-Z. Otherwise, c is treated as a
The literal ' C ' character.

\d matches a numeric character. equivalent to [0-9].

\d matches a non-numeric character. equivalent to [^0-9].

\f matches a page feed character. Equivalent to \x0c and \CL.

\ n matches a newline character. Equivalent to \x0a and \CJ.

\ r matches a carriage return character. Equivalent to \x0d and \cm.

\s matches any white space character, including spaces, tabs, page breaks, and so on. Equivalent to
[\f\n\r\t\v].

\s matches any non-white-space character. equivalent to [^ \f\n\r\t\v].

\ t matches a tab character. Equivalent to \x09 and \ci.

\v matches a vertical tab. Equivalent to \x0b and \ck.

\w matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '.

\w matches any non word character. Equivalent to ' [^a-za-z0-9_] '.

\XN matches N, where n is the hexadecimal escape value. The hexadecimal escape value must be
A set of two digits long. For example, ' \x41 ' matches ' A '. ' \x041 ' is equivalent
In ' \x04 ' & ' 1 '. You can use ASCII encoding in regular expressions ...

\num matches num, where num is a positive integer. A reference to the match that was obtained.
For example, ' (.) \1 ' matches two consecutive identical characters.

\ n identifies a octal escape value or a back reference. If \ n before at least
Gets a subexpression, then N is a back-reference. Otherwise, if n is octal
Number (0-7), then N is an octal escape value.

\NM identifies a octal escape value or a back reference. If \nm before at least have
is preceded by at least nm to get the subexpression, then NM is
to the reference. If there are at least n fetches before the \nm, then n is followed by a text
A back reference to the word m. If all the preceding conditions are not satisfied, if both N and M are
Octal number (0-7), then \nm will match octal escape value nm.

\NML if n is an octal number (0-3), and both M and L are octal digits (0-
7), match the octal escape value NML.

\un matches N, where n is a Unicode word in four hexadecimal digits
Character. For example, \u00a9 matches the copyright symbol (?).



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.