Confirm repeated occurrence
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
{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 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.