Home  >  Article  >  Backend Development  >  Detailed introduction to regular expressions

Detailed introduction to regular expressions

零下一度
零下一度Original
2017-07-16 13:25:431246browse

RegularExpression is a special string pattern, used to match a set of strings, just like using a mold to make products, and regular It is this mold that defines a rule to match characters that match the rules.

Regular expression \w \s \d \b
. Matches any character except newlines
\w matches letters or numbers or underscores or Chinese characters equivalent to '[^A-Za-z0-9_]'.
\s Matches any whitespace character
\d Matches digits
\b Matches the beginning or end of a word
^ Matches the beginning of a string
$ Matches the end of a string

\wWhether it can match Chinese characters depends on your operating system and your application environment

I believe many people know about regular expressions, but many people’s first feeling is that it is difficult Because when I first saw it, I felt that there was no pattern at all, and it was all a bunch of special symbols, completely incomprehensible.

In fact, it’s just because you don’t understand regular rules. Once you understand them, you will find that that’s it. There are actually not many related characters used in regular rules, and they are not difficult to remember, let alone understand. The only difficulty is the combination. After getting started, the readability is relatively poor and it is not easy to understand. The purpose of this article is to let everyone have a basic understanding of regular expressions, be able to understand simple regular expressions, and write simple regular expressions to meet daily needs. Just meet the needs under development.

What is a regular expression

A regular expression is a special string pattern used to match a set of strings. It's like using a mold to make a product, and regular rules are this mold, defining a rule to match characters that match the rules.

After consulting many related regular descriptions, I found that the definition of \w refers to containing uppercase and lowercase letters, numbers and underscores, which is equivalent to ([0-9a-zA-Z]) (Based on the classic regular expression 30-minute introductory tutorial)

However, in actual use, it is found that this is not the case. It can also be said that it actually includes more than just ([0-9a -zA-Z_])These data,

The following is the test screenshot tool for editplus:

actually except ([0 -9a-zA-Z_]) also includes Greek letters, Russian letters, etc.;

So if you need to pay attention to the specific matching data when registering user information, you must not use it directly \w It’s over

The above is the detailed content of Detailed introduction to regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn