Regular expressions in PHP (2 ). Are you sure you already know how to match a letter or number until now, but in more cases, you may need to match a word or a group of numbers. A word has several definite duplicates.
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 cluster meaning
^ [A-zA-Z _] $ all letters and underscores
^ [[: Alpha:] {3} $ all 3-letter words
^ A $ letter
^ A {4} $ aaaa
^ A {2, 4} $ aa, aaa or aaaa
^ A {1, 3} $ a, aa or aaa
^ A {2, }$ contains more than two a Strings
^ A {2,} for example, aardvark and aaab, but not apple
A {2,} such as 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? Let's take a look: It starts with an optional negative sign (\-{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 that contain 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.
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 has several...