Special symbols in a regular expression in PHP

Source: Internet
Author: User
Tags alphabetic character

Characters
Meaning: For a character, it is usually expressed as literal meaning that the next character is a special character and is not interpreted.
For example:/b/matches the character ' B ', by adding a backslash in front of B, which is/b/, the character becomes a special character, indicating
Match the dividing line of a word.
Or:
For several characters, the usual description is special, which indicates that the characters immediately followed are not special, but should be interpreted literally.
For example: * is a special character that matches any character (including 0 characters); For example:/a*/means matching 0 or more aces. To match the literal *, precede a with a backslash; For example:/a*/matches ' A * '.

Character ^
Meaning: The character that represents the match must be at the front.
For example:/^a/does not match "an A," in the ' a ', but matches "an A." The "A" in the front.

Character $
Meaning: Similar to ^, matches the last character.
For example:/t$/does not match ' t ' in ' eater ', but matches ' t ' in ' eat '.

Characters
Meaning: matches the preceding character 0 or n times.
For example:/bo*/matches ' B ' in ' boooo ' or ' a bird warbled ' in ' a ghost booooed ', but does not match ' agoat G '
Any characters in the runted ".

Character +
Meaning: Matches the character preceding the + sign 1 or n times. Equivalent to {1,}.
For example:/a+/matches ' a ' and ' Caaaaaaandy ' in ' Candy '. All ' a ' in the.

Character?
Meaning: match the preceding character 0 or 1 times.
For example:/e?le?/matches ' el ' and ' angle ' in ' Angel '. In the ' Le '.

Character.
Meaning: (decimal point) matches all the individual characters except the line break.
For example:/.n/matches "Nay, an apple was on the tree" in the ' an ' and ' on ', but does not match ' nay '.


Character (x)
Meaning: Match ' x ' and record the matching value.
For example:/(foo)/Match and record "Foo bar." In the ' foo '. The matched substring can be in the result array of the vegetarian [1], ..., [n] Return
Back, or the property of the RegExp object, ..., returns.

Character X│y
Meaning: Match ' x ' or ' Y '.
For example:/green│red/matches ' green ' and ' Red apple ' in ' green apple '. In the ' Red '.

Character {n}
Meaning: n Here is a positive integer. Matches the preceding n characters.
For example:/a{2}/does not match ' a ' in ' Candy, ' but matches all ' a ' and ' Caaandy ' in ' Caandy, '. In front of the two ' a '.

Character {n,}
Meaning: n Here is a positive integer. Matches at least n preceding characters.
For example:/a{2,} does not match ' a ' in ' Candy ', but matches all ' a ' and ' Caaaaaaandy ' in ' Caandy '. All ' a ' in the

Character {n,m}
Meaning: Both N and m are positive integers. Matches at least N of the characters up to M preceding.
For example:/a{1,3}/does not match any characters in "Cndy", but matches "candy," in the first two of ' a ', ' Caandy, '
' A ' and ' Caaaaaaandy ' in front of the three ' a ', note: even if "Caaaaaaandy" has a lot of ' a ', but only match the previous three ' a ' is "AAA".

character [XYZ]
Meaning: A list of characters that matches any one of the characters listed. You can use hyphens-to indicate a range of characters.
For example: [ABCD] is the same as [a-c]. They match ' C ' in ' B ' and ' ache ' in ' brisket '.

character [^XYZ]
Meaning: A character complement, that is, it matches everything except the listed characters. You can use hyphens-to indicate a range of characters.
For example: [^ABC] and [^a-c] are equivalent, they first match ' R ' and ' chop ' in ' brisket '. In the ' H '.

Character
Meaning: Match a space (do not confuse with B)

Character B
Meaning: Match the dividing line of a word, such as a space (do not confuse)
For example:/bnw/matches ' no ' in ' Noonday ',/wyb/matches ' possibly yesterday. ' In the ' ly '.

Character B
Meaning: Match the non-dividing line of a word
For example:/wbn/matches ' on ' in "Noonday",/ybw/matches "possibly yesterday." In the ' ye '.

Character CX
Meaning: The x here is a control character. Matches the control character of a string.
For example:/cm/matches a control-m in a string.

Character D
Meaning: Match a number, equivalent to [0-9].
For example:/d/or/[0-9]/matches "B2 is the suite number." In the ' 2 '.

Character D
Meaning: matches any non-number, equivalent to [^0-9].
For example:/d/or/[^0-9]/matches "B2 is the suite number." In the ' B '.

Character F
Meaning: Match a single form character

Character N
Meaning: Match a line break

Character R
Meaning: Match a carriage return character

Character S
Meaning: Match a single white space character, including spaces, tab,form feeds, line breaks, equivalent to [FNRTV].
For example:/sw*/matches "foo bar." In the ' bar '.

Character S
Meaning: matches a single character except white space, equivalent to [^ FNRTV].
For example:/s/w* matches "foo bar." In the ' foo '.

Character T
Meaning: Match a tab

Character V
Meaning: Match a head tab

Character W
Meaning: Matches all numbers and letters as well as underscores, equivalent to [a-za-z0-9_].
For example:/w/matches "Apple," in the ' a ', ". 28," In the ' 5 ' and ' 3D. ' In the ' 3 '.

Character W
Meaning: Matches other characters except numbers, letters, and underscores, equivalent to [^a-za-z0-9_].
For example:/w/or/[^ $A-za-z0-9_]/match "50%." In the '% '.

Character N
Meaning: n Here is a positive integer. Matches the value of N of the last substring of a regular expression (counting the left parenthesis).

For example:/apple (,) sorange1/matches "Apple, orange, cherry, peach." In the ' Apple, Orange ', here's a more complete example.
Note: If the number in the left parenthesis is smaller than the number specified by N, then N takes a line of octal escape as a description.

Characters Ooctal and Xhex
Meaning: The ooctal here is an octal escape value, while Xhex is a hexadecimal escape value that allows the embedding of ASCII code in a regular expression

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

Character description
\
The next character is marked with a special character, or a literal character, or a back reference, or an octal escape character. For example, ' n ' matches the character "n". ' \ n ' matches a line break. The sequence ' \ ' matches "" and "\ (" Matches "(".
^
Matches the starting position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after ' \ n ' or ' \ R '.
$
Matches the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before ' \ n ' or ' \ R '.
*
Matches the preceding subexpression 0 or more times. For example, zo* can match "z" and "Zoo". * Equivalent to {0,}.
+ matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.
?
Matches the preceding subexpression 0 or one time. For example, "Do (es)?" can match "do" in "do" or "does".? 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,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
{N,m}
Both M and n are non-negative integers, where n <= m. Matches at least n times and matches up to M times. Liu, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' O? '. Note that there can be no spaces between a comma and two numbers.
?
When the character immediately follows any other restriction (*, +,?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "oooo", ' o+? ' will match a single "O", while ' o+ ' will match all ' o '.
.
Matches any single character except "\ n". To match any character including ' \ n ', use a pattern like ' [. \ n] '.
(pattern)
Match pattern and get this match. The obtained matches can be obtained from the resulting Matches collection, the Submatches collection is used in VBScript, and {CONTENT} is used in JScript ... Property. To match the parentheses character, use ' \ (' or ' \ ').
(?:p Attern)
Matches pattern but does not get a matching result, which means that this is a non-fetch match and is not stored for later use. This is useful when using the "or" character (|) to combine parts of a pattern. For example, ' Industr (?: y|ies) is a more abbreviated expression than ' industry|industries '.
(? =pattern)
Forward-checking matches the lookup string at the beginning of any string that matches the pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, ' Windows (? =95|98| nt|2000) ' Can match Windows 2000 ', but does not match Windows 3.1 in Windows. Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.
(?! Pattern
Negative to pre-check, in any mismatch negative lookahead matches the search string at any point where a string of not matching pattern begins to match the lookup strings. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example ' Windows (?! 95|98| nt|2000) ' can match Windows 3.1 ', but does not match Windows 2000 in Windows. Pre-check does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check
X|y
Match x or Y. For example, ' Z|food ' can match "z" or "food". ' (z|f) Ood ' matches "Zood" or "food".
[XYZ]
The character set is combined. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in ' plain '.
[^XYZ]
Negative character set. Matches any character that is not contained. For example, ' [^ABC] ' can match ' P ' in ' plain '.
[A-z]
The character range. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the ' a ' to ' Z ' range.
[^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 within the range of ' a ' to ' Z '.
\b
Matches a word boundary, which is the position between a word and a space. For example, ' er\b ' can match ' er ' in ' never ', but not ' er ' in ' verb '.
\b
Matches a non-word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '.
\cx
Matches the control character indicated by X. For example, \cm matches a control-m or carriage return. The value of x must be one of a-Z or a-Z. Otherwise, c is treated as a 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 break. Equivalent to \x0c and \CL.
\ n
Matches a line break. Equivalent to \x0a and \CJ.
\ r
Matches a carriage return character. Equivalent to \x0d and \cm.
\s
Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s
Matches any non-whitespace 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
Match N, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. For example, ' \x41 ' matches ' A '. ' \x041 ' is equivalent to ' \x04 ' & ' 1 '. ASCII encoding can be used in regular expressions:
\num
Matches num, where num is a positive integer. A reference to the obtained match. For example, ' (.) ' matches two consecutive identical characters.
\ n
Identifies an octal escape value or a back reference. N is a back reference if \ n has at least one of the first obtained sub-expressions. Otherwise, if n is the octal number (0-7), N is an octal escape value.
\nm
Identifies an octal escape value or a back reference. If at least \nm was preceded by at least nm, then NM is a back reference. If there are at least N fetches before \nm, then N is a back reference followed by the literal m. If none of the preceding conditions are met, if both N and M are octal digits (0-7), then \nm will match the octal escape value nm.
\nml
If n is an octal number (0-3) and both M and L are octal digits (0-7), the octal escape value NML is matched.
\un
Match N, where N is a Unicode character represented by four hexadecimal digits. For example, \u00a9 matches the copyright symbol (?).

Special symbols in a regular expression in PHP

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.