Use regular expressions in php

Source: Internet
Author: User
Regular expressions are important in all programming languages. php also provides regular expressions that can be used to search for and replace strings, basically, we can use java regular expressions on php. Regular Expressions 1. Common regular expressions w: letters and numbers

Regular expressions are important in all programming languages. php also provides regular expressions that can be used to search for and replace strings. Basically, we can use java regular expressions in php.

1. Regular expression

1. Common regular expressions

\ W indicates letters, numbers, and underscores

\ W indicates non-letters, numbers, and underscores

\ D indicates a number

\ D indicates a non-digit

\ S indicates all blank characters (line breaks, spaces, etc)

\ S indicates all non-blank characters

^ The beginning of a row (used for demarcation)

$ End of a row (used to define)

. Match any character except line breaks

[Abc] characters a, B, c

[^ Abc] any character except a, B, and c

[A-zA-Z0-9] represents a combination of letters and numbers

\ N indicates line feed

\ Indicates the backslash


2. number representation (X indicates a set of specifications)

X? It can appear 0 or 1 time

X + can appear once or multiple times

X * can appear 0, once, or multiple times

X {n} must appear n times

X {n,} must appear more than n times

X {n, m} must appear n to m times


3. logical operators (X and Y indicate a set of specifications)

Xy x followed by Y

(X) as a capture group specification (subexpression)

X | y x specification or Y specification


II. how to use regular expressions in php

1. use regular expressions to find characters in fixed formats

Php provides the preg_match () and preg_match_all () methods for us to use. These two methods can be used for string format validation and search.

(1) retrieve all four digits in the string

Before using the regular expression, make sure that the regular expression is written in '/P/I' at the position of P. take the four-digit format as an example. the code is as follows:

Echo "retrieve all four digits in the string"; $ str1 = "abcd1034yu3029"; preg_match_all ('/\ d {4}/I', $ str1, $ arr1 ); foreach ($ arr1 [0] as $ val) {echo "$ val "."
";}

It should be noted that $ arr1 is a two-dimensional array. php places all matching characters in this array.


(2) extract strings in formats such as 1111 and 3333.

In this case, we need to use a word expression. the code is as follows:

Echo "the four digits in the extracted string must be the same for each number, for example, 1111, 4444, 5555"; $ str2 = "abcd1034yu3029sd3333"; preg_match_all ('/(\ d) \ 1 {3}/I ', $ str2, $ arr2); foreach ($ arr2 [0] as $ val) {echo "$ val "."
";}
The following is the running effect of the two code segments:

(3) retrieve a string in a format similar to 1111-2222-3333

Echo "retrieves a string in a format similar to 1111-2222-3333"; $ str3 = "ab3333-5555-6666cd1034yu3029sd3333"; preg_match_all ('/(\ d) \ 1 {3}-(\ d) \ 2 {3}-(\ d) \ 3 {3}/I ', $ str3, $ arr3); foreach ($ arr3 [0] as $ val) {echo "$ val "."
";}

If you are interested, use print_r () to print the $ arr3, which contains two arrays. The first is the matched string, and the second is the value of the word expression.

(4) to get the last four digits of a string in the format of 1111-232-3333 and 1111-575-3333.

Echo "retrieves the last 4 digits of a string in a format similar to 1111-232-3333, 1111-575-3333"; $ str4 = "ab3333-575-6666cd10348888-464-9999"; preg_match_all ('/(\ d) \ 1 {3}-(\ d) \ d \ 2-(\ d) \ 3 {3}/I ', $ str4, $ arr4 ); foreach ($ arr4 [0] as $ val) {$ nums = explode ("-", $ val); echo "$ nums [2]"."
";}


(5) filter repeated characters VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> echo "filter duplicate characters"; $ repeatstr = "I love and peace"; preg_match_all ('/(\ w) \ 1 */iu ', $ repeatstr, $ arr); $ finalStr = ""; foreach ($ arr [1] as $ val) {$ finalStr. = $ val ;}
/I after the U table shows a specification, indicating matching UTF-8 characters, there are several other you can view php development documentation

2. there is also a complicated match called loop view matching.

Expression

Description

(? <= Expression)

The reverse direction is certainly the loop view, indicating that the left side of the location can match the Expression.

(?

Negative backward view, indicating that the left side of the location cannot match the Expression

(? = Expression)

The order is certainly the loop, indicating that the Expression can be matched on the right of the location

(?! Expression)

Sequential negative view, indicating that the right side of the location cannot match the Expression


This part is a bit complicated to understand. you can refer to the following article. An example of stripping intermediate content is not bad.

Http://blog.csdn.net/lxcnn/article/details/4304754


Next, paste the entire php page I wrote.

 ";} Echo" the four digits in the extracted string must be the same for each number, for example, 1111, 4444, 5555 "; $ str2 =" abcd1034yu3029sd3333 "; preg_match_all ('/(\ d) \ 1 {3}/I', $ str2, $ arr2); foreach ($ arr2 [0] as $ val) {echo "$ val "."
";}Echo" retrieves a string in a format similar to 1111-2222-3333 "; $ str3 =" ab3333-5555-6666cd1034yu3029sd3333 "; preg_match_all ('/(\ d) \ 1 {3}-(\ d) \ 2 {3}-(\ d) \ 3 {3}/I ', $ str3, $ arr3); foreach ($ arr3 [0] as $ val) {echo "$ val "."
";} Echo" retrieves the last 4 digits of a string in a format similar to 1111-232-3333, 1111-575-3333 "; $ str4 =" ab3333-575-6666cd10348888-464-9999 "; preg_match_all ('/(\ d) \ 1 {3}-(\ d) \ d \ 2-(\ d) \ 3 {3}/I ', $ str4, $ arr4 ); foreach ($ arr4 [0] as $ val) {$ nums = explode ("-", $ val); echo "$ nums [2]"."
";} Echo" verify if it is an email "; $ email =" chen@163.com "; preg_match_all ('/^ \ w + @ \ w + \. \ w + $/I ', $ email, $ earr); if (count ($ earr [0])> 0) {echo "$ email ". "is a valid email";} else {echo "$ email ". "Illegal email" ;}?>





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.