Learn regular expressions with examples in PHP_PHP Tutorial
Learn regular expressions by looking at examples
First of all, let us look at two special characters: '^' and '$'. They are used to match the beginning and end of strings respectively. Here are examples:
First, let’s take a look at two special characters: '^' and '$'. They are used to match the beginning and end of a string respectively. Here are some examples:
"^The": Matches strings starting with "The";
"of despair$": matches strings ending with "of despair";
"^abc$": matches strings starting with abc and Strings ending with abc are actually only matched by abc;
"notice": matches strings containing notice;
You can see if you don't use what we mentioned Two characters (the last example) means that the pattern (regular expression) can appear anywhere in the string being tested, you are not locking it to both sides.
There are also several characters '*', '+', and '?', which are used to indicate the number of times or order that a character can appear. They respectively represent: "zero or more", "one or more", and "zero or one." Here are some examples:
"ab*": Matches the string a and 0 or more b ("a", "ab" , "abbb", etc.);
"ab+": Same as above, but with at least one b ("ab", "abbb", etc.);
"ab? ": Matches 0 or one b;
"a?b+$": Matches a string ending with one or 0 a plus one or more b.
You can also Limit the number of characters appearing in curly brackets, for example
"ab{2}": matches an a followed by two b (no less) ("abb");
"ab{2,}": at least two b("abb", "abbbb", etc.);
"ab{3,5}": 2-5 b("abbb ", "abbbb", or "abbbbb").
You should also note that you must always specify (i.e, "{0,2}", not "{,2}"). Likewise, You must notice that '*', '+', and '?' are the same as the following three range annotations, "{0,}", "{1,}", and "{0,1}" respectively .
Now put a certain number of characters in parentheses, for example:
"a(bc)*": matches a followed by 0 or one "bc";
"a(bc){1,5}": one to 5 "bc."
There is also a character '│', which is equivalent to OR operation:
"hi│ hello": matches strings containing "hi" or "hello";
"(b│cd)ef": matches strings containing "bef" or "cdef";
"(a│b)*c": Matches a string containing - multiple (including 0) a or b, followed by a string of c;
A dot ('.') can Represents all single characters:
"a.[0-9]": an a followed by a character followed by a number (strings containing such a string will be matched, this bracket will be omitted in the future)
"^.{3}$": ends with three characters. The content enclosed in square brackets only matches a single character
"[ab]": matches a single a or b (same as "a│b");
"[a-d]": matches a single character from 'a' to 'd' (same as "a│b│c│d" and "[abcd ]"The effect is the same);
"^[a-zA-Z]": Matches a string starting with a letter
"[0-9]%": Matches a string of the form x % string
",[a-zA-Z0-9]$": Matches a string ending with a comma plus a number or letter
You can also put what you don’t want The characters are listed in brackets, you only need to use '^' as the beginning of the bracket (i.e., "%[^a-zA-Z]%" matches two percent signs with a non-letter character string).
In order to be able to explain, but when "^.[$()│*+?{" is used as a character with special meaning, you must add '' in front of these characters, and in In php3 you should avoid using it at the beginning of the pattern. For example, the regular expression "($│?[0-9]+" should be called ereg("($│?[0-9]+", $str ) (I don’t know if php4 is the same)
Don’t forget that the characters inside the brackets are an exception to this rule - inside the brackets, all special characters, including (''), will lose them special properties (i.e., "[*+?{}.]" matches strings containing these characters). Also, as the regx manual tells us: "If the list contains ']', it is best to treat it as a list the first character in (may follow '^'). If it contains '-', it is best to put it at the front or at the end, or at the second end point of a range (i.e. [a-d-0 -9] The '-' in the middle will be valid.
For completeness, I should cover collating sequences, character classes, and equivalence classes. But I don't want to go into too much detail in these aspects, which are below This article doesn't need to be covered. You can get more information in the regex man pages.
How to build a pattern to match the currency amount input
Okay, now we are going to use our Use what you learned to do something useful: construct a matching pattern to check whether the input information is a number representing money.We think there are four ways to represent the amount of money: "10000.00" and "10,000.00", or without a decimal part, "10000" and "10,000". Now let's start building this matching pattern:
^[ 1-9][0-9]*$
This means that all variables must start with a non-0 number. But this also means that a single "0" cannot pass the test. Here is the solution:
^(0│[1-9][0-9]*)$
"Only 0 and numbers not starting with 0 match", we can also allow a negative Before the number:
^(0│-?[1-9][0-9]*)$
This is: "0 or a number starting with 0 may have a negative The number with the sign in front of it." Okay, okay now let's be less strict and allow it to start with 0. Now let's drop the minus sign , since we don't need it when representing coins. We now specify the pattern to use Match the decimal part:
^[0-9]+(.[0-9]+)?$
This implies that the matched string must start with at least one Arabic digit. But note , in the above pattern "10." is not matched, only "10" and "10.2" are acceptable. (Do you know why)
^[0-9]+(.[0-9 ]{2})?$
We specified above that there must be two decimal places after the decimal point. If you think this is too harsh, you can change it to:
^[0-9]+( .[0-9]{1,2})?$
This will allow one or two characters after the decimal point. Now we add commas (every third digit) to increase readability, We can express it like this:
^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$
Don't forget that the plus sign '+' can be replaced by the multiplication sign '*' if you want to allow blank strings to be entered (why?). Also don't forget that the backslash ''' may be used in PHP strings An error occurred (a very common error). Now that we can confirm the string, we can now remove all commas str_replace(",", "", $money) and then treat the type as double and we can pass He did the math.
Constructing a regular expression to check email
Let’s continue discussing how to verify an email address. There are three parts in a complete email address: POP3 Username ( Everything to the left of '@'), '@', the server name (that's the rest). Usernames can contain uppercase and lowercase letters, Arabic numerals, periods ('.'), minus signs ('-'), and underscores ('_'). Server names also comply with this rule, except of course the underscore.
Now, usernames cannot start and end with periods. The same goes for servers. And you cannot have two consecutive periods. There is at least one character between them, so now let’s take a look at how to write a matching pattern for the username:
^[_a-zA-Z0-9-]+$
Now The existence of periods is not allowed. We add it:
^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$
The above means: "Start with at least one standard character (except . and unexpected), followed by 0 or more strings starting with a dot."
To simplify it a bit, we can use eregi () replaces ereg().eregi() which is case insensitive, we don’t need to specify two ranges "a-z" and "A-Z" – only one needs to be specified:
^[_a- z0-9-]+(.[_a-z0-9-]+)*$ The server name after
is the same, but the underscore must be removed:
^[a-z0- 9-]+(.[a-z0-9-]+)*$
Done. Now just use "@" to connect the two parts:
^[_a-z0- 9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$
This is complete Email authentication matching pattern, just call
eregi('^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9- ]+(.[a-z0-9-]+)*$ ',$eamil)
to get whether it is email.
Other uses of regular expressions
Extracting strings
ereg() and eregi() have a feature that allows users to extract part of a string through regular expressions (You can read the manual for specific usage). For example, we want to extract the file name from path/URL – the following code is what you need:
ereg("([^/]*)$", $pathOrUrl , $regs);
echo $regs[1];
Advanced substitution
ereg_replace() and eregi_replace() are also very useful: if we want to All spaced negative signs are replaced with commas:
ereg_replace("[ ]+", ",", trim($str));

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
