PHP Regular Expression basic function modifier metacharacters and escape characters

Source: Internet
Author: User
Tags php regular expression alphanumeric characters

I used a lot of regular expressions in the thieves, so I will gradually reduce the regular expressions in the next version.


The characters to be escaped in PHP regular expressions are as follows:

$ ^ * () + = {} [] |/: <> .? '"

Note: expressions in the perl style must start and end with/, for example,/food/table matching character food

The perl modifier is as follows:

I. Complete case-insensitive search
G. Find all occurrences (all occurrences, complete global search)
M treats a string as multiple rows (m indicates multiple (multiple )). By default, the ^ and $ characters match the start and end of the string. Use the m modifier to match the beginning of each line in the string ^ and $
S treats a string as a line and ignores all linefeeds. It is opposite to the m modifier.
X ignore blank spaces and comments in Regular Expressions
U stops after the first match. By default, the last matching character is found. This modifier can be used to stop the first match. Then the form matches cyclically.

Metacharacters:

Using Perl regular expressions can also be used to search for matching. Metacharacter is a letter with a backslash in front, indicating a special meaning. The following are some useful metacharacters.

A Only matches the start of A string
B matching word boundary
B. match any character beyond the word boundary.
D matches the number character, which is the same as [0-9]
D. Match non-numeric characters.
S matches white space characters
S matches non-blank characters
[] Enclose a character class, including: [0-9] [a-z] [a-zA-Z] and so on.
() Enclose a character group or define a anti-Reference
$ Match the end of a row
^ Match the beginning of a row
* Matches the previous subexpression zero or multiple times. To match * characters, use *
+ Match the previous subexpression once or multiple times. To match + characters, use +
? Match the previous subexpression zero or one time, or specify a non-Greedy qualifier?
. Match any character except line breaks
Next metacharacters
W matches any string that only contains underscores and alphanumeric characters, which is the same as [a-zA-Z0-9 _]
W: match strings without underscores and numbers

Perl functions:

Array preg_grep (string pattern, array input [, flags])
Searches all elements in an array and returns an array consisting of all elements matching a certain pattern.
PHP 4.3 adds an optional parameter flag, which accepts a value PREG_GREP_INVERT and passes this flag to obtain data elements that do not match this pattern.

Int preg_match (string pattern, string [, array matches [, int flags [, int offset])
Returns TRUE if the string is in the search mode. Otherwise, FALSE is returned.
Optional. The matches parameter can contain all the sub-modes contained in the search mode. By default, a matched string is returned. If () is enclosed by a child, it is output after the array.

Int preg_match_all (string pattern, string, array pattern_array [, int order])
Like the preg_match function, preg_match only searches once, while preg_match_all executes the cyclic search and returns all matching results.

Mixed preg_replace (mixed pattern, mixed replacement, mixed str [, int limit])
Replacement is used to replace all occurrences of pattern, and the modified result is returned.
The optional limit parameter specifies the number of matching times. If limit is not set or-1 is set, all the situations will be replaced.


The above are common functions, as well as detailed explanations such as preg_quote, preg_replace_callbak, and preg_split...

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.