Regular expression character cluster (i)

Source: Internet
Author: User
what is a character cluster?

In programs in the Internet, regular expressions are often used to validate the user's input. When the user submits a form, to determine whether the input phone number, address, email address, credit card number, etc. is valid, with ordinary literal-based characters is not enough.

So to use a more liberal way of describing the pattern we want, it's a character cluster. To create a character cluster that represents all vowel characters, place all the vowels in a square bracket:

[Aaeeiioouu]

This pattern matches any vowel character, but can only represent one character. A hyphen can be used to represent a range of characters, such as:

[A-z]//Match all lowercase letters

[A-z]//Match all uppercase letters

[A-za-z]//Match all the letters

[0-9]//Match all the numbers

[0-9\.\-]//Match all numbers, periods and minus signs

[\f\r\t\n]//match all whitespace characters

Similarly, these also represent only one character, which is a very important one. If you want to match a string consisting of a lowercase letter and a single digit, such as "Z2″," "T6″," or "G7″, but not" Ab2″, "R2d3″, or" b52″, use this pattern:

^[a-z][0-9]$

Although [A-z] represents a range of 26 letters, here it can only match a string with the first character being a lowercase letter.

The previous mention of ^ represents the beginning of a string, but it has another meaning. When used in a set of square brackets ^ is, it means "non" or "exclude" meaning, often used to remove a character. Also with the previous example, we require that the first character cannot be a number:

^[^0-9][0-9]$

This pattern is matched to "&5″," "G7″," and " -2″, but not to" 12″, "66″. Here are a few examples of excluding specific characters:

[^a-z]//All characters except lowercase letters

[^\\\/\^]//all characters except (\) (/) (^)

[^\ "\ ']//all characters except double quotation marks (") and single quotation marks (')

Special character "." (point, period) is used in regular expressions to denote all characters except the "New line". So the pattern "^.5$" matches any two-character string that ends with the number 5 and begins with other non-"new line" characters. Mode "." You can match any string, except for an empty string, and to include only a "new line" of strings.

   The regular expressions for PHP have some built-in universal character clusters, which are listed below:

Character cluster meaning

[[: Alpha:]] any letter

[[:d Igit:]] any number

[[: Alnum:]] Any letters and numbers

[[: Space:]] any whitespace character

[[: Upper:]] Any capital letter

[[: Lower:]] any lowercase letter

[[:p UNCT:]] any punctuation

[[: Xdigit:]] Any 16 binary number, equivalent to [0-9a-fa-f]

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.