PHP Regular Expressions (2)

Source: Internet
Author: User
Tags ereg php regular expression expression engine

PHP Regular Expressions (2)

Space Character
To match spaces in a search string, we use the pre-defined Posix class [[: space]. square brackets indicate the relevance of consecutive characters. ": space:" indicates the class to be matched (in this case, it is any blank character ). White spaces include tab characters, new line characters, and blank characters. Alternatively, if the search string must contain only one space, rather than a tab or a new line character, you can use a space character (""). In most cases, I tend to use ": space:", because it means that my intention is not just a single space character, which is easily ignored. Here are some Posix-standard pre-defined classes,
There are some Posix-standard pre-defined classes that can be used as regular expressions, including [: alnum:], [: digit:], [: lower:], and so on. The complete list can be viewed here

We can match a single blank character like this:

<? Php echo ereg ("Mitchell [[: space:] Harper", "Mitchell Harper");?>
We can also use? To tell the Regular Expression Engine that there is no blank or blank matching.
<? Php echo ereg ("Mitchell [[: space:]? Harper "," MitchellHarper ");?>

Mode group
The related patterns can be separated in square brackets. It is easy to use [a-z] and [A-Z] to specify that only one lower-case letter or one column of upper-case letters exist in part of the search string.
<? Php
// Lowercase letters are required from the first to the last
Echo ereg ("^ [a-z] + $", "johndoe"); // return true
?>
Or Image
<? Php
// The first and last are uppercase letters.
Ereg ("^ [A-Z] + $", "JOHNDOE"); // returns true?
?>
We can also tell the Regular Expression Engine that we want to use either lower-case letters or upper-case letters. We just need to combine the [a-z] and [A-Z] patterns.
<? Php echo ereg ("^ [a-zA-Z] + $", "JohnDoe");?>
In the above example, if we can match "John Doe" instead of "JohnDoe", it will be very meaningful. We use the following regular expression to do this:
^ [A-zA-Z] + [[: space:] {1} [a-zA-Z] + $
It is easy to search for a numeric string
<? Php echo ereg ("^ [0-9] + $", "12345");?>

Word grouping
Not only can the search mode be grouped, but we can also use parentheses to group related search words.
<? Php echo ereg ("^ (John | Jane). + $", "John Doe");?>
In the above example, we have a string header character followed by "John" or "Jane", at least one other character, and then a string tail character. So...
<? Php echo ereg ("^ (John | Jane). + $", "Jane Doe");?>
... Will also match our search mode

Special characters
Because some characters are used in a clear grouping or syntax in the search mode, such as parentheses in (John | Jane), we need to tell the Regular Expression Engine to block these characters, process them to make them part of the searched string, not part of the search expression. The method we use is called "character escape", which involves adding a backslash to any "special symbol. So, for example, if I want to include '|' in my search, I can do this.
<? Php echo ereg ("^ [a-zA-z] + \ | [a-zA-z] + $", "John | Jane");?>
Here are only a few characters you want to escape. You must escape ^, $, (,),., [, | ,*,?, +, \ And {.

I hope you will feel a little bit more powerful about regular expressions. Now let's look at two examples of checking a string in data using a regular expression.

Regular Expression example
Example 1
Let's make the first example quite simple. Check a standard URL. A standard URL (without a port number) consists of three parts:

[Protocol]: // [domain name]

Let's start from the Protocol part that matches the URL and make it only use http or ftp. We can use the following regular expression to do this:

^ (Http | ftp)
^ Character refers to the header of a string. It enclose http and ftp with parentheses and separate them with the "or" symbol (|, we tell the Regular Expression Engine that either http or ftp must start with a string.

A domain name is usually composed of www.somesite.com, but you can select the www part at will. For the sake of simplicity, we only allow. com,. net, and. org domain names to be considered. We 'd better represent the domain name in the regular expression as follows:
(Www \.)?. + \. (Com | net | org) $
When we put everything together, our regular expression can be used to check a domain name, such:

<? Php
Function isValidDomain ($ domainName)
{

Return ereg ("^ (http | ftp): // (www \.)?. + \. (Com | net | org) $ ", $ domainName );
}
// True (true)
Echo isValidDomain ("http://www.somesite.com ");
// True (true)
Echo isValidDomain ("ftp://somesite.com ");
// False)
Echo isValidDomain ("ftp://www.somesite.fr ");
// False)
Echo isValidDomain ("www.somesite.com ");
?>

Example 2
Because I live in Sydney, Australia, let's check a typical Australian international phone number. The format of the Australian international phone number is as follows:
+ 61x xxxx-xxxx
The first x is the area code, and the others are telephone numbers. Check the phone number that starts with '+ 61' and follows a zone number between 2 and 9. We use the following regular expression:

^ \ + 61 [2-9] [[: space:]
Note that the above search mode uses '\' to escape the '+' character, so that it can be included in the search without being interpreted as a regular expression. [2-9] tells the Regular Expression Engine that we need to include a number between 2 and 9. [[: Space:] indicates that the regular expression is expected to have a blank space.

Here is the remaining search mode for phone numbers:
[0-9] {4}-[0-9] {4} $
There is nothing unusual here. We just tell the Regular Expression Engine the number available for the phone number. It must be a combination of four numbers followed by a connector, followed by a combination of four other numbers, and then a string's tail character.
Put the complete regular expression together and put it into a function. We can use the code to check some Australian international phone numbers:
<? Php
Function isValidPhone ($ phoneNum)
{
Echo ereg ("^ \ + 61 [2-9] [[: space:] [0-9] {4}-[0-9] {4} $ ", $ phoneNum );
}
// True (true)
Echo isValidPhone ("+ 619 0000-0000 ");
// False)
Echo isValidPhone ("+ 60 00000000 ");
// False)
Echo isValidPhone ("+ 611 00000000 ");
?>

Summary
Regular Expressions use code that is not suitable for writing and repeating to check a string. In the last few pages, we have explained the basics of all Posix standard regular expressions, including characters, grouping and PHP ereg functions. We also know how to use regular expressions to check some simple strings in PHP.

Articles you may be interested in
  • PHP Regular Expressions (1)
  • PHP Regular Expressions (3)
  • PHP Regular Expression collection
  • How to Write efficient regular expressions
  • Regular expression syntax
  • Summary of common Regular Expressions
  • Sort regular expressions that are frequently used
  • Use JavaScript to verify the regular expression of the mobile phone number

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.