PHP Regular expression basic function modifier metacharacters character and need escape word

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags array basic default expression find function modifier multiple

The characters you need to escape in the PHP tutorial regular expressions are:

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

Note: The Perl-style expression requires a/start and end, such as:/food/table-matching characters food

The Perl modifiers are as follows:

I completes a case-insensitive search
G Find all occurrences (all occurrences, complete global search)
M treats a string as multiple lines (m means multiple (listbox)). By default, the ^ and $ characters match the start and end of the string. Using the M modifier will make the ^ and $ match the start of each line in the string
s treats a string as a row, ignoring all line breaks in it; it's just the opposite of the M modifier
x ignores whitespace and annotations in regular expressions
U stop after the first match. By default, the last matching character result is found. This modifier can be used to stop after the first match. And then the form loops match.

Metacharacters Description:

Another useful thing to do with Perl regular expressions is to search for matches using a variety of meta characters. Metacharacters (metacharacter) is an alphabetic character preceded by a backslash that denotes a particular meaning. Here are some useful meta characters.

A matches only the beginning of a string
B Match Word boundary
b matches any character outside the word boundary
D matches the numeric character, which is the same as [0-9]
D matching non-numeric characters
s matching whitespace characters
S-matching non-whitespace characters
[] surrounds a character class that includes: [0-9] [A-z] [a-za-z], and so on.
() surround a character group or define a counter reference
$ match end of line
^ Match beginning of Line
* Match the preceding subexpression 0 or more times. To match the * character, use the *
+ matches the preceding subexpression one or more times. to match the + character, use the +
? Match the preceding subexpression 0 or more times, or indicate a non-greedy qualifier?
. Matches any character other than a newline
Leads to the next meta character
W matches any string that contains only underscore and alphanumeric characters, which is the same as [a-za-z0-9_]
W matches strings without underscore and alphanumeric characters

The Perl style functions are:

Array Preg_grep (string pattern, array input [, flags])
Searches for all elements in an array, returning an array of all elements that match a pattern
php 4.3 Adds an optional parameter flag that accepts a value Preg_grep_invert passing this flag will get data elements that do not match the pattern.

int Preg_match (string pattern, string string [, array matches [, int flags [, int offset]]]
Searches for a pattern in a string and returns False if there is a return true.
Optional input parameter matches can contain parts of the child mode contained in the search pattern. The default returns a matching string that, when surrounded by a () child, is output after the array.

int Preg_match_all (string pattern, string string, array pattern_array [, int order])
Like function Preg_match, preg_match only searches once, and Preg_match_all performs a circular search that returns all matching results.

Mixed preg_replace (mixed pattern, mixed replacement, mixed str [, int limit])
Replaces all occurrences of pattern with replacement and returns the modified result.
Optional limit specifies how many times the match should occur. Not setting limit or setting to-1 replaces all occurrences.

Related Article

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.