Regular expression in PHP (2)

zhaozj2021-02-16  54

Regular expression in PHP (2)

Source: phpuser.com determines that the repeated appearance now, you already know how to match a letter or number, but more cases, you may have to match a word or a set of numbers. A word has several letters, a set of numbers have several singletons. Follow the rack ({}) behind the character or character cluster to determine the number of repetitive appearances of the previous content.

Character clustering meaning ^ [a-za-z _] $ all letters and underline ^ [[: alpha:]] {3} $ All 3 letters word ^ 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 ^ a {2,} such as: Aardvark and AAAB, but Apple does not line a {2,} such as: Baad and AAA, but Nantucket does not line T {2} two tabs. {2} All two characters describe three different uses of curly brackets. A number, {x} means "the front character or character cluster only appears"; a digital plus comma, {x,} means "the previous content" X or more times "; two The comma-separated numbers, {x, y} representation "The front content appears at least X times, but does not exceed Y." We can extend the pattern to more words or numbers:

^ [A-ZA-Z0-9 _] {1,} $ // All contain more than one letter, number or underscore string ^ [0-9] {1,} $ // all positive ^ - { 0, 1} [0-9] {1,} $ // All integers ^ - {0, 1} [0-9] {0,}. {0, 1} [0-9] {0,} $ // All the last example of the decimal is not very good, is it? That's here: with all the 1 optional negative (- {0, 1}), followed by 0 or more numbers ([0-9] {0,}), and an optional The decimal point (. {0, 1}) follows up to 0 or more numbers ([0-9] {0,}), and there is no other things ($). Below you will know a simpler way to use. Special characters "?" Are equal to {0, 1}, which are all represented: "0 or 1 previous content" or "the previous content is optional." So just now the example can be simplified:

^ -? [0-9] {0,}.? [0-9] {0,} $ special characters "*" and {0,} are equal, they all represent "0 or more front content". Finally, the characters " " are equal, indicating "1 or more previous content", so the four examples above can be written:

^ [A-ZA-Z0-9 _] $ / / all contain more than one letter, number or underscore string ^ [0-9] $ // all positive ^ -? [0-9] $ // All integers ^ -? [0-9] *.? [0-9] * $ // All scales Of course, this does not significantly reduce the complexity of regular expressions, but can make them easier read.

转载请注明原文地址:https://www.9cbs.com/read-19111.html

New Post(0)