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:
- [[: Alpha:]// Any letter
- [[: Digit:]// Any number
- [[: Alnum:]// Any letter or number
- [[: Space:]// Any white characters
- [[: Upper:]// Any uppercase letter
- [[: Lower:]// Any lowercase letter
- [[: Punct:]// Any punctuation
- [[: 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
- ^ [A-zA-Z _] $// All letters and underscores
- ^ [[: Alpha:] {3} $// All three letters of words
- ^ A $// Letter
- ^ A {4} $// Aaaa
- ^ A {2, 4} $// Aa, aaa, or aaaa
- ^ A {1, 3} $// A, aa or aaa
- ^ A {2,} $// String containing more than two a strings
- ^ A {2 ,}// For example, aardvark and aaab, but not apple
- A {2 ,}// For example, baad and aaa, but not Nantucket
- T {2}// Two tabs
- . {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:
- ^ [A-zA-Z0-9 _] {1,} $// All strings containing more than one letter, number, or underline
- ^ [0-9] {1,} $// All positive numbers
- ^-{0, 1} [0-9] {1,} $// All integers
- ^-{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:
- ^-?[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:
- ^ [A-zA-Z0-9 _] + $
- // All strings containing more than one letter, number, or underline
- ^ [0-9] + $// All positive numbers
- ^ -? [0-9] + $// All integers
- ^ -? [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.