Analysis on the specific implementation of PHP Regular Expression matching

Source: Internet
Author: User
Tags php regular expression

What is the specific implementation of PHP Regular Expression matching? In fact, we know that in the actual matching process, we operate more than just a single letter or number. What should we do when we face words or numbers?

PHP Regular Expression matching uses PHP Regular Expression built-in general character clusters. Which PHP Regular Expression built-in general character set?

PHP Regular Expression built-in general character set and its meaning:

 
 
  1. [[: Alpha:]// Any letter 
  2. [[: Digit:]// Any number 
  3. [[: Alnum:]// Any letter or number 
  4. [[: Space:]// Any white characters 
  5. [[: Upper:]// Any uppercase letter 
  6. [[: Lower:]// Any lowercase letter 
  7. [[: Punct:]// Any punctuation 
  8. [[: Xdigit:]// Any hexadecimal number, which is equivalent to [0-9a-fA-F] 

Analysis of PHP Regular Expression matching:

Until now, you know how to match a letter or number, but in more cases, you may need to match a word or a group of numbers. A word may consist of several letters, and a group of numbers may consist of several singular numbers. Braces ({}) following the character or character cluster are used to determine the number of occurrences of the preceding content.

Character Set and meaning of the PHP regular expression used

 
 
  1. ^ [A-zA-Z _] $// All letters and underscores 
  2. ^ [[: Alpha:] {3} $// All three letters of words 
  3. ^ A $// Letter 
  4. ^ A {4} $// Aaaa 
  5. ^ A {2, 4} $// Aa, aaa, or aaaa 
  6. ^ A {1, 3} $// A, aa or aaa 
  7. ^ A {2,} $// String containing more than two a strings 
  8. ^ A {2 ,}// For example, aardvark and aaab, but not apple 
  9. A {2 ,}// For example, baad and aaa, but not Nantucket 
  10. T {2}// Two tabs 
  11. . {2}// All two characters 

These examples describe three different usages of curly brackets. A number, {x} indicates "the character or character cluster appears only x times"; a number is added with a comma, {x ,} "The preceding content appears x or more times"; two numbers separated by commas (,). {x, y} indicates that "the preceding content appears at least x times, but not more than y times ". We can extend the pattern to more words or numbers:

 
 
  1. ^ [A-zA-Z0-9 _] {1,} $// All strings containing more than one letter, number, or underline 
  2. ^ [0-9] {1,} $// All positive numbers 
  3. ^-{0, 1} [0-9] {1,} $// All integers 
  4. ^-{0, 1} [0-9] {0,}. {0, 1} [0-9] {0,} $// All decimals 

The last example is hard to understand, right? Take a look: start with an optional negative number (-{0, 1}) (^), followed by 0 or more numbers ([0-9] {0 ,}) and an optional decimal point (. {0, 1}) followed by 0 or multiple numbers ([0-9] {0,}), and nothing else ($ ). Next you will know the simpler method that can be used.

Special Character "? "It is equal to {0, 1}, and both represent" 0 or 1 previous content "or" previous content is optional ". So the example just now can be simplified:

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

The special characters "*" and {0,} are equal. They all represent "0 or multiple front content ". Finally, the character "+" is equal to {1,}, indicating "one or more previous content". Therefore, the preceding four examples can be written as follows:

 
 
  1. ^ [A-zA-Z0-9 _] + $
  2. // All strings containing more than one letter, number, or underline 
  3. ^ [0-9] + $// All positive numbers 
  4. ^ -? [0-9] + $// All integers 
  5. ^ -? [0-9] *.? [0-9] * $// All decimals 

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

The specific implementation of PHP Regular Expression matching will be introduced here, hoping to help you understand and learn the specific implementation of PHP Regular Expression matching.


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.