Until now, you already know how to match a letter or number, but more often than not, you might want to match a word or a set of numbers. A word consists of several letters, and a set of numbers has several singular components. The curly braces ({}) that follow a character or a cluster of characters are used to determine how many occurrences of the preceding content are repeated.
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 strings of a
^a{2,} such as: Aardvark and Aaab, but not Apple.
A{2,} such as: Baad and AAA, but Nantucket not.
\T{2} two tabs
. {2} all two characters
These examples describe the three different uses of curly braces. A number, {x}, means "the preceding character or cluster of characters appears only x times"; A number with a comma, {x,} means "x or more times before"; two comma-delimited numbers, {x,y} indicates that "previous 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 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? So see: With all the optional minus sign (\-{0,1}) preceded (^), followed by 0 or more digits ([0-9]{0,}), and an optional decimal point (\.{ 0,1}) followed by 0 or more digits ([0-9]{0,}) and nothing else ($). Below you will know the simpler way to use it.
Special character "?" is equal to {0,1}, they all represent: "0 or 1 previous content" or "previous content is optional." So the example can be simplified as follows:
^\-? [0-9] {0,}\.? [0-9] {0,}$
The special character "*" is equal to {0, and they all represent "0 or more preceding content". Finally, the character "+" is equal to {1}, representing "1 or more preceding content", so the above 4 examples can be written as follows:
^[a-za-z0-9_]+$//All strings containing more than one letter, number, or underscore
^[0-9]+$//All positive numbers
^\-? [0-9]+$//all integers
^\-? [0-9]*\.? [0-9]*$//All Decimal
This does not, of course, technically reduce the complexity of regular expressions, but it makes them easier to read.
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