4th Chapter-php Regular Expression of data processing-Zheng Achi (continued) _php Foundation

Source: Internet
Author: User
Tags alphabetic character ereg numeric lowercase mixed php file php regular expression posix
1. Basic knowledge of regular expressions
Meaning: A string pattern consisting of ordinary characters and (A-Z) and some special characters
function: Validation.
Replaces text.
Extracts a substring from a string.
Category: POSIX and Perl
The POSIX style is easier to master, but not for binary mode, and Perl is relatively complex.
2.POSIX-style regular expressions
1. Writing Regular Expressions
Table 4.3 POSIX regular expression syntax format list

Characters

Description

\

An escape character that is used to escape special characters. For example, '. ' Matches a single character, ' \. ' Matches a point number. ' \-' matching hyphen '-', ' \ \ ' match symbol ' \ '

^

Matches the start position of the input string. For example, ' ^he ' denotes a string that begins with ' he '

$

Matches the end position of the input string. For example, ' ok$ ' represents a string ending with ' OK '

*

Matches the preceding subexpression 0 or more times. For example, ' zo* ' can match "z" and "Zoo". * Equivalent to {0,}

+

Matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" and "Zoo", but cannot match "Z". + equivalent to {1,}

?

Match the preceding subexpression 0 times or once. For example, ' Do (es)? ' You can match "do" in "do" or "does". '?' Equivalent to {0,1}

{n}

N is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two ' o ' in ' food '

{n,}

N is a non-negative integer. Match at least N times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ' but can match all ' o ' in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '

{n,m}

m and n are nonnegative integers, where nm. Matches N times at least and matches up to M times. For example, "o{1,3}" will match the first three ' o ' in "Fooooood". ' o{0,1} ' is equivalent to ' o '. Note that you cannot have spaces between commas and two numbers

?

When the character is immediately following any of the other qualifiers (*, +,?, {n}, {n,}, {n,m}), the matching pattern is not greedy. Non-greedy patterns match as few strings as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "oooo", ' o+? ' Will match a single "O", and ' o+ ' will match all ' o '

.

Matches any single character other than "\ n" to match any character including ' \ n ', you can use the ' [. \ n] ' mode

(pattern)

Match pattern and get this match. The obtained match is saved to the appropriate array. To match the parentheses character, use ' \ (' or ' \ '

(?:p Attern)

Matches pattern but does not get a matching result, which means it is a non fetch match and is not stored. This is in use "or" | " is useful when you combine parts of a pattern. For example, ' Industr (?: y|ies). It's a simpler expression than ' industry|industries '.

(? =pattern)

Forward lookup, matching the find string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, ' Windows (? =95|98| nt|2000) ' Can match windows in Windows 2000, but cannot match windows in Windows 3.1. It does not consume characters, that is, after a match occurs, the next matching search begins immediately after the last match, instead of starting after the character that contains the pre-check.

(?! Pattern

Negative pre-check, matches the lookup string at the beginning of any mismatched pattern string. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, ' Windows (?! 95|98| nt|2000) ' matches windows in Windows 3.1, but does not match windows in Windows 2000. It does not consume characters, that is, after a match occurs, the next matching search begins immediately after the last match, instead of starting after the character that contains the pre-check.

X|y

Match x or Y. For example, ' Z|food ' can match ' z ' or ' food ', ' (z|f) Ood ' matches ' zood ' or ' food '

[XYZ]

Character set combination. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in ' plain '

[^XYZ]

Negative character set combination. Matches any characters that are not included. For example, ' [^ABC] ' can match ' P ' in ' plain '

[A-z]

The range of characters. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the range ' a ' to ' Z '

[^a-z]

Negative character range. Matches any character that is not in the specified range. For example, ' [^a-z] ' can match any character that is not in the range ' a ' to ' Z '

Here are some examples of simple regular expressions:
' [a-za-z0-9] ': All capitals, lowercase letters, and numbers from 0 to 9 are represented.
' ^hello ': A string representing the start of hello.
' world$ ': A string that represents the end of the world.
'. At ': A string, such as "Cat", "Nat", that starts with any single character other than "\ n" and ends with "at".
' ^[a-za-z] ': Represents a string that begins with a letter.
' Hi{2} ': After the letter H followed by two I is hii.
' (GO) + ': A string that represents at least one ' go ' string, such as ' Gogo '
The ID card number usually consists of 18 digits or 17 digits followed by an X or Y letter, to match the ID number, you can write:
^[0-9]{17} ([0-9]| X| Y) $
The regular expression of an email address can be written:
^[a-za-z0-9\-]+@[a-za-z0-9\-]+\. [A-za-z0-9\-\.] +$
2. String matching
Ereg () and eregi () functions
You can use the Ereg () function to find a match between a string and a substring, return the length of a matching string, and return an array of matching characters by using parameters. The syntax format is as follows:
int Ereg (String ($pattern), string $string [, array $regs])
Copy Code code as follows:

<?php
/* This example checks whether the string is an ISO-formatted date (YYYY-MM-DD) * *
$date = "1988-08-09";
$len =ereg ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ', $date, $regs);//date format is YYYY-MM-DD
if ($len)
{
echo "$regs [3]. $regs [2]. $regs [1]". "<br>"; Output "09.08.1988"
echo $regs [0]. " <br> "; Output "1988-08-09"
Echo $len; Output 10
}
Else
{
echo "Wrong date format: $date";
}
?>

3. Replacement of strings
The ereg_replace () function syntax is formatted as follows:
String Ereg_replace (String $pattern, String $replacement, String $string)
Description: The function uses the string $replacement to replace the part that matches the $pattern in the string $string and returns the replaced string. If no match is found, return as-is
Copy Code code as follows:

<?php
$str = "Hello World";
Echo ereg_replace (' [Aeo] ', ' x ', $str). "<br>"; Output ' Hxllx Wxrld '
$res = ' <a href=\ ' hello.php\ ' >hello</a> ';
echo ereg_replace (' Hello ', $res, $STR); Replace ' hello ' with hyperlink replacement
?>

4. Split array

You can use the split () function to do the same function as the explode () function, and you can split the string according to the regular expression given, and return an array. The syntax format is as follows:

Array split (string $pattern, string $string [, int $limit])

5. Generating Regular Expressions

3.PERL-compliant regular expressions

1. Writing Regular Expressions

Table 4.4 Perl compatible Regular expression extended syntax format

Characters

Description

\b

Matches a word boundary, which is the position between the word and the space. For example, ' er\b ' can match ' er ' in ' never ', but cannot match ' er ' in ' verb '

\b

Matches a non-word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '

\cx

Matches the control character indicated by X. For example, ' \cm ' matches a control-m or carriage return character. The value of x must be one of a~z or a~z. Otherwise, the ' C ' is treated as a literal ' C ' character

\d

Matches a numeric character. Equivalent to ' [0-9] '

\d

Matches a non-numeric character. Equivalent to ' [^0-9] '

\f

Matches a page feed character. Equivalent to ' \x0c ' and ' \cl '

\ n

Matches a line feed character. Equivalent to ' \x0a ' and ' \CJ '

\ r

Matches a carriage return character. Equivalent to ' \x0d ' and ' \cm '

\s

Matches any white space character, including spaces, tabs, page breaks, and so on. Equivalent to ' [\f\n\r\t\v] '

\s

Matches any non-white-space character. Equivalent to ' [^ \f\n\r\t\v] '

\ t

Matches a tab character. Equivalent to ' \x09 ' and ' \ci '

\v

Matches a vertical tab. Equivalent to ' \x0b ' and ' \ck '

\w

Matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '

\w

Matches any non word character, equivalent to ' [^a-za-z0-9_] '

\xn

Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be a determined two digits long. For example, ' \x41 ' matches ' A '. ' \x041 ' is equivalent to ' \x04 ' & ' 1 '. ASCII encoding can be used in regular expressions

\num

Matches num, where num is a positive integer. A reference to the match that was obtained. For example, ' (.) \1 ' matches two consecutive identical characters

\ n

Flags a octal escape value or a back reference. If there are at least N obtained subexpression before \ n is a back reference. Otherwise, if n is an octal number (0~7), then N is an octal escape value

\nm

Flags a octal escape value or a back reference. NM is a back reference if there are at least nm to get the subexpression before \nm. If there are at least N fetches before \nm, then N is a back reference followed by a literal m. If all the preceding conditions are not satisfied, if both N and M are octal digits (0~7), then \nm will match the octal escape value NM

\nml

If n is an octal number (0~3) and both M and L are octal digits (0~7), then the octal escape value is matched NML

\un

Matches n, where N is a Unicode character represented by 4 hexadecimal digits. For example, ' \u00a9 ' matches the copyright symbol (©)

2. String matching
Preg_match () function to find the string, the syntax format is as follows:
int Preg_match (string $pattern, String $subject [, array $matches [, int $flags]])
Note: The structure of the function is similar to the Ereg () function and searches the $subject string for what matches the regular expression given by $pattern.
The Preg_match () function returns the number of occurrences of the $pattern. Not 0 times (no match) is 1 times, because the Preg_match () function stops the search after the first match
Another is Preg_match_all (), which continues the search from the end of the first match until the entire string has been searched.
The value of the Preg_match_all () function parameter $flags can take the following three kinds:
Preg_pattern_order. The default entry, which represents the array that $matches[0] matches for all patterns,
$matches [1] is an array of strings that match the child patterns in the first bracket, and so on.
Preg_set_order. If you set this tag, $matches[0] is an array of the first set of matches, $matches [1] as an array of the second set of matches, and so on.
Preg_offset_capture. Preg_offset_capture can be combined with two other tags,
If you set this tag, the matching result for each occurrence also returns its subordinate string offset.
3. Replacement of strings
Use the Preg_replace () function to complete the same function as the function ereg_replace (), find a matching substring in the string, and replace the substring with the specified string.
The syntax format is as follows:
Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
4. Segmentation of Strings
The Preg_split () function can use regular expressions to split a string as a boundary and to return a substring to an array, similar to the split () function.
The syntax format is as follows:
Array Preg_split (String $pattern, string $subject [, int $limit [, int $flags]])
Note: This function is case-sensitive and returns an array containing substrings that are split along the bounds that match the $pattern in the $subject.
The $limit is an optional parameter, and if specified, the $limit string is returned, if omitted or-1, there is no limit.
The $flags value can be the following three types:
Preg_split_no_empty. If this tag is set, the function returns only a non-empty string.
Preg_split_delim_capture. If this tag is set, the match for the bracket expression in the delimiter pattern is also captured and returned.
Preg_split_offset_capture. If you set this tag, the matching result for each occurrence also returns its subordinate string offset.
4.3 Instance-Validating form content
"Example 4.4" uses regular expressions to verify that the content of the form entered by the user satisfies the formatting requirements.
Create a new ex4_4_hpage.php file and enter the following code.
Copy Code code as follows:

<?php
Include ' ex4_4_hpage.php '; Include File ex4_4hpage.php
$id =$_post[' id '];
$pwd =$_post[' pwd '];
$phone =$_post[' phone '];
$Email =$_post[' Email '];
$checkid =preg_match ('/^\w{1,10}$/', $id); Check if the string is within 10 characters
$checkpwd =preg_match ('/^\d{4,14}$/', $pwd); Check if there is a number between 4~14
$checkphone =preg_match ('/^1\d{10}$/', $phone); Check if the 11-digit number begins with 1
Check the legality of your email address
$checkEmail =preg_match ('/^[a-za-z0-9_\-]+@[a-za-z0-9\-]+\.[ A-za-z0-9\-\.] +$/', $Email);
if ($checkid && $checkpwd && $checkphone && $checkEmail)//If all is 1, the registration is successful
echo "Registered successfully! ";
Else
echo "Registration failed, wrong format";
?>

Create a new ex4_4_ppage.php file and enter the following code:
2. String matching
Preg_match () function to find the string, the syntax format is as follows:
int Preg_match (string $pattern, String $subject [, array $matches [, int $flags]])
Note: The structure of the function is similar to the Ereg () function and searches the $subject string for what matches the regular expression given by $pattern.
The Preg_match () function returns the number of occurrences of the $pattern. Not 0 times (no match) is 1 times, because the Preg_match () function stops the search after the first match
Another is Preg_match_all (), which continues the search from the end of the first match until the entire string has been searched.
The value of the Preg_match_all () function parameter $flags can take the following three kinds:
Preg_pattern_order. The default entry, which represents the array that $matches[0] matches for all patterns,
$matches [1] is an array of strings that match the child patterns in the first bracket, and so on.
Preg_set_order. If you set this tag, $matches[0] is an array of the first set of matches, $matches [1] as an array of the second set of matches, and so on.
Preg_offset_capture. Preg_offset_capture can be combined with two other tags,
If you set this tag, the matching result for each occurrence also returns its subordinate string offset.
3. Replacement of strings
Use the Preg_replace () function to complete the same function as the function ereg_replace (), find a matching substring in the string, and replace the substring with the specified string.
The syntax format is as follows:
Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
4. Segmentation of Strings
The Preg_split () function can use regular expressions to split a string as a boundary and to return a substring to an array, similar to the split () function.
The syntax format is as follows:
Array Preg_split (String $pattern, string $subject [, int $limit [, int $flags]])
Note: This function is case-sensitive and returns an array containing substrings that are split along the bounds that match the $pattern in the $subject.
The $limit is an optional parameter, and if specified, the $limit string is returned, if omitted or-1, there is no limit.
The $flags value can be the following three types:
Preg_split_no_empty. If this tag is set, the function returns only a non-empty string.
Preg_split_delim_capture. If this tag is set, the match for the bracket expression in the delimiter pattern is also captured and returned.
Preg_split_offset_capture. If you set this tag, the matching result for each occurrence also returns its subordinate string offset.
4.3 Instance-Validating form content
"Example 4.4" uses regular expressions to verify that the content of the form entered by the user satisfies the formatting requirements.
Create a new ex4_4_hpage.php file and enter the following code.
Copy Code code as follows:

<?php
Include ' ex4_4_hpage.php '; Include File ex4_4hpage.php
$id =$_post[' id '];
$pwd =$_post[' pwd '];
$phone =$_post[' phone '];
$Email =$_post[' Email '];
$checkid =preg_match ('/^\w{1,10}$/', $id); Check if the string is within 10 characters
$checkpwd =preg_match ('/^\d{4,14}$/', $pwd); Check if there is a number between 4~14
$checkphone =preg_match ('/^1\d{10}$/', $phone); Check if the 11-digit number begins with 1
Check the legality of your email address
$checkEmail =preg_match ('/^[a-za-z0-9_\-]+@[a-za-z0-9\-]+\.[ A-za-z0-9\-\.] +$/', $Email);
if ($checkid && $checkpwd && $checkphone && $checkEmail)//If all is 1, the registration is successful
echo "Registered successfully! ";
Else
echo "Registration failed, wrong format";
?>

Create a new ex4_4_ppage.php file and enter the following code:
Copy Code code as follows:

<?php
include ' ex4_4_hpage.php ';//include file ex4_4hpage.php
$id =$_po st[' ID '];
$pwd =$_post[' pwd '];
$phone =$_post[' phone '];
$Email =$_post[' Email '];
$checkid =preg_match ('/^\w{1,10}$/', $id);/check whether the string is within 10 characters
$checkpwd =preg_match ('/^\d{4,14}$/', $pwd); Check for
$checkphone =preg_match ('/^1\d{10}$/', $phone) between 4-14 characters;//check to see if the 11-digit number begins with 1
//Check the legality of the email address
$ Checkemail=preg_match ('/^[a-za-z0-9_\-]+@[a-za-z0-9\-]+\.[ A-za-z0-9\-\.] +$/', $Email);
if ($checkid && $checkpwd && $checkphone && $checkEmail)//If all is 1, the registration successful
Echo registration is successful! ";
Else
Echo "registration failed with the wrong format";
?>

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.