Basic Regular Expression Method

Source: Internet
Author: User

As we all know, regular expressions greatly facilitate many PHP programmers in dealing with strings.
However, for beginners who want to learn regular expressions, there are more or less problems. I would like to explain the following more or less to help them :)

Generally, regular expressions use the following "symbols ":
^-Indicates that the character string must contain such a word, such as ^ http: //, indicating that the character string must contain http ://
$-Indicates that the string must be followed by such a word.
? (A question mark)-It represents one or no characters
* (One *)-It represents no or more front characters
. (A period)-It represents any character
+ (One plus)-It represents at least one or more front characters
[Xyz]-It represents any character, or X, or Y, or Z
[A-Z]-It represents any character, from A to Z
[[: Alnum:]-represents from A to Z, 0 to 9
[[: Digit:]-represents from 0 to 9

When using regular expressions, note that ereg and eregi can be significantly different ~
Ereg is very sensitive (meaning that all uppercase and lowercase letters are clearly identified), while eregi is not (as long as I stands for case-insensitive)

Here are some examples:

$ Regexp = eregi ("? C "," ABC "); // -- this is correct, because? It can represent "one" or no character
$ Regexp = eregi ("? C "," AC "); // -- this is also true, because? It can represent one or "no" Character
$ Regexp = eregi ("? C "," A "); // -- this is incorrect. Even if there is a, c is still required.

$ Regexp = eregi ("[ABC]", "A"); // -- this is correct, because a is a character in braces.
$ Regexp = eregi ("[A-Z]", "C"); // -- this is correct because C is included in A to Z.
$ Regexp = eregi ("[A-Z]", "0"); // -- this is incorrect because 0 is not included in a to Z.

$ Regexp = eregi ("a. c", "ABC"); // -- this is correct, because a. represents the word "any ".
$ Regexp = eregi ("a. c", "AC"); // -- this is incorrect. Because. represents a "character, it is incorrect to put abbc.

$ Regexp = eregi ("A + C", "aaaac"); // -- this is correct, because + represents one or more characters
$ Regexp = eregi ("A + C", "abbc"); // -- this is incorrect. + represents one or more "first characters ", instead of representing any character
$ Regexp = eregi ("A + C", "ABC"); // -- this is also incorrect. The comment is as follows:

$ Regexp = eregi ("[^ ABC]", "A"); // -- this is different. If ^ appears in, it indicates that all the words in "except" are correct.
$ Regexp = eregi ("[^ ABC]", "D"); // -- this is correct because D is not in ABC.
$ Regexp = eregi ("[^ [: alnum:]", "9"); // -- this is correct, because ~ Not included in a to Z, 0 to 9
Of course, [[: digit:] is also used in the same way:
$ Regexp = eregi ("[^ [: digit:]", "A"); // -- this is correct, because a is not included in 0 to 9.

Finally, a useful example is provided,
For example, I don't want everyone's password to have 0 to 9. You can try it like this:
$ Regexp = eregi ("^ [^ 0-9] + $", "aaa9"); // -- use aaa9 as the password
If ($ Regexp = ""){
Echo "the password has a number. Please change it ";
} Else {
Echo "no password, pass ";
}

The test shows that the password has a number. Please change it.
I believe everyone can see that [^ 0-9] each has a ^ and $, to ensure that the front and back cannot have a number, while + ensures that there is no number in the middle,
[] ^ In it, it is "except 0 to 9, all others pass"
So if you take the 9 command, it will show "The password has no number, pass!

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.