现在的位置: 首页 > Web设计> 正文
一种更符合自然语意的PHP正则表达式
2013年08月15日 Web设计 暂无评论 ⁄ 被围观 3,797+

在编程的过程中,尤其是文本的处理,我们很多时候都需要正则表达式,比如,验证用户输入的Email是否格式正确,电话号码是否符合规范,或者其他的自定义规则。对新手来说,熟记正则表达式的规则比较困难,那么有没有一种更符合自然语义的正则表达式呢,本文会给你答案。

verbalexp

Most newbie (and some seasoned) programmers have difficultly constructing Regular Expressions. Many a times one needs to create a Regexp quickly to test a a particular piece of code. However, not being comfortable withe Regexps can be a problem. VerbalExpressions is a PHP library that enables you to construct regular expressions using natural language like constructs. Think of it like a DSL for building Regexps.
verbalexpressions 已经实现了多个语言版本的库,可以从这里得到。

Below is a sample PHP code that constructs a regular expression which tests whether a given string is a valid url.

如下示例用来测试给定的一个string是否是有效的url地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
 
include_once('VerbalExpressions.php');
 
$regex = new VerEx;
 
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();
 
if($regex->test("http://www.codediesel.com"))
    echo "valid url";
else
    echo "invalid url";
 
?>
<?php

include_once('VerbalExpressions.php');

$regex = new VerEx;

$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();

if($regex->test("http://www.codediesel.com"))
    echo "valid url";
else
    echo "invalid url";

?>

The main part of the code the DSL like interface that helps you build a Regexp.

1
2
3
4
5
6
7
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();

If you want to see what regular expression the code has built, we can use the getRegex function of the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
include_once('VerbalExpressions.php');
 
$regex = new VerEx;
 
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();
 
echo $regex->getRegex();
<?php

include_once('VerbalExpressions.php');

$regex = new VerEx;

$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();

echo $regex->getRegex();

This will print the Regexp given below.

打印输出正则表达式。

/^(http)(s)?(\:\/\/)(www\.)?([^ ]*)$/m

We can now use the above Regexp in our code to accomplish the same thing as above.

1
2
3
4
5
6
7
$myRegexp = '/^(http)(s)?(\:\/\/)(www\.)?([^ ]*)$/m';
 
if (preg_match($myRegexp, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}
$myRegexp = '/^(http)(s)?(\:\/\/)(www\.)?([^ ]*)$/m';

if (preg_match($myRegexp, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}

We can also use the $regex object given in the above example directly in our code where the particular Regexp is required.

可以使用函数 getRegex 获取正则表达式规则,或者直接可以用 $regex 对象,库内部已经实现了转换。

内部的转换函数:

1
2
3
4
5
6
7
8
9
10
11
12
/**
* Object to string
*
* PHP Magic method to return a string representation of the object.
*
* @access public
* @return string
*/
    public function __toString()
    {
        return $this->getRegex();
    }
/**
* Object to string
*
* PHP Magic method to return a string representation of the object.
*
* @access public
* @return string
*/
    public function __toString()
    {
        return $this->getRegex();
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
include_once('VerbalExpressions.php');
 
$regex = new VerEx;
 
$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();
 
if (preg_match($regex, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}
include_once('VerbalExpressions.php');

$regex = new VerEx;

$regex  ->startOfLine()
        ->then("http")
        ->maybe("s")
        ->then("://")
        ->maybe("www.")
        ->anythingBut(" ")
        ->endOfLine();

if (preg_match($regex, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}

The PHP version of VerbalExpressions is a port of the original JavaScript version,JSVerbalExpressions. Additional modifiers that will help you build regular expressions can be found here.

文章节选:constructing-hard-regular-expressions-with-verbalexpressions

给我留言

留言无头像?


×
腾讯微博