Very important PHP Regular expression detailed, PHP regular expression _php tutorial

Source: Internet
Author: User
Tags php regular expression uppercase letter

Very important PHP Regular expression detailed, PHP regular expression


Regular expressions are a very important point of knowledge in PHP, often used to find and replace strings, the most common is to verify that the user input information format is correct, such as message format, phone format and so on. There are also such as collectors and other software, the regular is also necessary!
Now start learning the basic syntax of regular expressions:
1. "/" is the delimiter, and the part between the "/" delimiter is the pattern that will be matched in the target object. at the same time, in order to be more flexible, the introduction of meta-characters, namely "+", "*", and "?".
(1) the "+" metacharacters specify that their leading characters must appear one or more times in the target object
For example:/php+/, can match "phpp", that is, the letter ph followed by one or more letters p string matches.
(2) the "*" metacharacters specify that their leading characters must appear 0 times or more consecutively in the target object
For example:/php*/can Match "PHPDDT", that is, there can be 0 or more p at the back of the ph
(3) "?" A meta-character specifies that its leading object must appear 0 or more times in the target object.
For example:/php?/can "Pher" match, that is, PHH can have 0 or 1 p
Other important metacharacters:

    • \s: used to match a single space character, including Tab key and line break;
    • \s: used to match all characters except a single space character;
    • \d: Used to match numbers from 0 to 9;
    • \w: used to match letters, numbers, or underscore characters;
    • \w: used to match all characters that do not match \w;
    • .: Used to match all characters except the line break.

Example:/\s+/is used to match one or more space characters in the target object
2. Locators are used to specify where the matching pattern appears in the target object. Commonly used are "^", "$", "\b" and "\b"
(1) "^" Locator to specify that the matching pattern must be at the beginning of the target string
(2) "$" locator specifies that the matching pattern must be at the end of the target object
(3) \ The B locator specifies that the matching pattern must be one of the two boundaries at the beginning or end of the target string
(4) The "\b" locator specifies that the matching object must be at the beginning and end of the target string within two bounds
3.php Regular matching mode is very flexible, You can specify a range of
for example:
/[a-z]/
The above regular expression will match any uppercase letter from a to Z range.
/[a-z]/
The above regular expression will match any lowercase letter from a to Z range.
/[0-9]/
The above regular expression will match any number in the range from 0 to 9.
/([a-z][a-z][0-9]) +/
The above regular expression will be associated with any string of letters and numbers
4. You can match multiple mode selections at the same time
such as/phpddt.com| Phpddt|100/can match "phpddt.com" "PHPDDT" "100" to
5. The negation "[^]" specifies that the specified string in the schema cannot exist in the target object
for example: [^PHPDDT] Match everything except the PHPDDT character
here is the regular expression common function! (very important)

<?php//preg_match ("Regular expression", "string") is used to find matches in a string $email = "987044391@qq.com"; if (Preg_match ("/^ ([a-za-z0-9]) + ([. a-za-z0-9_-]) *@ ([. a-za-z0-9_-]) + ([. a-za-z0-9_-]+] + ([. a-za-z0-9_-]) $/", $email ) {  echo ' match succeeded
 
  '; }else {  echo ' match failed
 
  '; //preg_quote ("string") adds an escape character in front of each regular expression syntax, which is \  $str = "PHP Point-and-click is a php-learning site, (⊙o⊙) ..."; echo Preg_quote ($STR); echo "
 
  ";  Preg_split ("Regular", "string") splits the string $php = "+php++ Point ++++ is a good website"; $field = Preg_split ("/\+{1,}/", $php); foreach ($field as $f) {echo $f. "; } echo "
 
  "; Preg_grep ("Regular", "string") returns a new array after matching the array $phpddt = array ("PHP dot-pass", "php100", "hehe", "hahaha", "Phpchina"); $item = Preg_grep ("/^php/", $PHPDDT); Print_r ($item);  echo "
 
  "; Preg_replace ("Regular", "Replace content", "original string")  

The above is the whole content of this article, I hope that everyone's learning PHP regular expressions to help.

Articles you may be interested in:

    • PHP Extracts the image address in a string [regular expression]
    • PHP Regular Expression recommendations
    • The implementation and explanation of the regular expression of email address in PHP
    • A simple way for PHP to match URLs with regular expressions
    • PHP Regular expression matches any attribute in the IMG method
    • PHP uses regular expressions to extract strings in the string with angle brackets, parentheses, brackets, curly braces
    • PHP uses curl and regular expressions to crawl Web page data examples
    • PHP filter HTML tags, attributes, etc. regular expression summary
    • A detailed description of strings and regular expressions in PHP

http://www.bkjia.com/PHPjc/1088792.html www.bkjia.com true http://www.bkjia.com/PHPjc/1088792.html techarticle very important PHP regular expression in detail, php regular expression Jiu Zheng expression is a very important point of knowledge in PHP, often used to find and replace strings, the most common is to test ...

  • 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.