search
HomeBackend DevelopmentPHP TutorialLearn regular expressions with examples in PHP_PHP Tutorial

Learn regular expressions with examples in PHP_PHP Tutorial

Jul 21, 2016 pm 03:58 PM
phpandcharacterstudyExampleusregularlookexpressionfirst

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));

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317550.htmlTechArticleLook at examples to learn regular expressions. First, let us look at two special characters: '^' and '$ 'They are used to match the beginning and end of the string respectively. Here are examples: First, let...
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.