A brief analysis of the specific implementation of PHP regular expression matching _php tutorial

Source: Internet
Author: User
Tags php regular expression
What is the specific implementation of the PHP regular expression match? In fact, we know that in the actual matching operation of the process we operate not only a single letter or number, then we face like a word or a set of numbers when the time to deal with it?

The specific implementations of the PHP regular expression match are used in the PHP regular expression built-in universal character clusters, all of which have PHP regular expressions built-in universal character sets?

PHP Regular expression built-in universal Character set and meaning:

 
  
  
  1. Any letter
  2. Any number
  3. Any letters and numbers
  4. Any whitespace character
  5. Any capital letter
  6. Any lowercase letters
  7. Any punctuation

Analysis of PHP Regular expression matching:

So far, you already know how to match a letter or number, but more likely, you might want to match a word or a group of numbers. A word consists of several letters, and a group of numbers has several singular parts. The curly braces ({}) following the character or character cluster are used to determine the number of occurrences of the preceding content.

PHP Regular expression character set and meaning used

 
 
  1. ^[a-za-z_]$ //All letters and underscores
  2. ^[[:alpha:]]{3}$ //All 3-letter words
  3. ^a$ //Letter A
  4. ^a{4}$ //aaaa
  5. ^a{2,4}$ //aa,aaa or AAAA
  6. ^a{1,3}$ //a,aa or AAA
  7. ^a{2,}$ //contains more than two a strings
  8. ^a{2,} //such as: Aardvark and Aaab, but not Apple
  9. a{2,} //such as: Baad and AAA, but Nantucket not
  10. t{2} //Two tabs
  11. . {2} //All two characters

These examples describe the three different uses of curly braces. A number, {x}, means "the preceding character or character cluster appears only x times"; A number plus a comma, {x,} means "x or more occurrences of the preceding content", and two comma-delimited numbers, {x, y} means "the preceding content appears at least x times, but not more than Y". We can extend the pattern to more words or numbers:

 
  
  
  1. All strings that contain more than one letter, number, or underscore
  2. All positive numbers.
  3. All the integers

The last example is not very well understood, is it? Let's see: With all starts with an optional minus sign (-{0,1}), followed by 0 or more digits ([0-9]{0,}), and an optional decimal point (. { 0,1}) followed by 0 or more numbers ([0-9]{0,}), and nothing else ($). Below you will know the simpler way to use it.

Special characters "?" is equal to {0,1}, and they all represent: "0 or 1 preceding content" or "previous content is optional". So just the example can be simplified to:

 
  
  
  1. ^-? [0-9] {0,}.? [0-9] {0,}$

The special characters "*" are equal to {0,}, and they all represent "0 or more of the preceding content." Finally, the character "+" is equal to {1,}, which means "1 or more preceding contents", so the above 4 examples can be written as:

 
  
  
  1. ^[a-za-z0-9_]+$
  2. All strings that contain more than one letter, number, or underscore
  3. All positive numbers.
  4. All the integers

Of course, this does not technically reduce the complexity of regular expressions, but it makes them easier to read.

A specific implementation of the PHP regular expression match is introduced to you and hopefully helps you understand and learn about specific implementations of PHP regular expression matching.


http://www.bkjia.com/PHPjc/446576.html www.bkjia.com true http://www.bkjia.com/PHPjc/446576.html techarticle What is the specific implementation of the PHP regular expression match? In fact, we know that in the actual matching operation of the process we operate not only a single letter or number, then we face ...

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