Make sure the recurrence occurs
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.
Character cluster meaning
^[a-za-z_]$ all the letters and underscores
^[[:alpha:]]{3}$ all 3-letter words
^a$ Letter A
^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,} such as: Aardvark and Aaab, but not Apple
A{2,} such as: Baad and AAA, but Nantucket not
{2} two tab characters
. {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:
^[a-za-z0-9_]{1,}$//All strings that contain more than one letter, number, or underscore
^[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 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:
^-? [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:
^[a-za-z0-9_]+$//All strings that contain more than one letter, number, or underscore
^[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 formal expressions, but it makes them easier to read.
http://www.bkjia.com/PHPjc/508572.html www.bkjia.com true http://www.bkjia.com/PHPjc/508572.html techarticle make sure that the recurrence is up to now, you already know how to match a letter or number, but more of the case, you might want to match a word or a group of numbers. A word has a number of ...