现在我们希望看到一个字符串始于abc。正则表达式中匹配字符开头的元字符为脱字符号^。如果我们想看看字符串是否始于abc,我们可以这样来使用: [CODE=php]// create a string $string = 'abcdefghijklmnopqrstuvwxyz0123456789';
// try to match the beginning of the string if(preg_match("/^abc/", $string)) { // if it matches we echo this line echo 'The string begins with abc'; } else { // if no match is found echo this line echo 'No match found'; } ?>[/CODE] 从上面的代码中,我们看到会输出以下内容: The string begins with abc 正斜线是正则表达式的分界符,并用双引号包含。因此我们看到,使用元字符^会匹配字符串的开头,而不管它后面是什么字符。
四、如果我想要匹配时区分大小写,该怎么办呢?
如果你使用上面的代码匹配如下模式: if(preg_match(“/^ABC/ ",$string)) 那么该脚本会输出: No match found 这是由于搜索是大小写敏感的,'abc'和'ABC'是不一样的。如果我们想同时匹配'abc'和'ABC',我们需要使用一个修饰符来使得搜索不区分大小写。php中的正则表达式和大多数正则表达式一样,使用修饰符'i'来表示不区分大小写。现在我们的脚本可能看起来像这样: [CODE=php]// create a string $string = 'abcdefghijklmnopqrstuvwxyz0123456789';
// try to match our pattern if(preg_match("/^ABC/i", $string)) { // echo this is it matches echo 'The string begins with abc'; } else { // if not match is found echo this line echo 'No match found'; } ?>[/CODE] 现在脚本能够匹配到abc,当然也能匹配到abc、ABC、Abc、aBc……。在本教程后面会有更多的修饰符介绍。
// try to match our pattern at the end of the string if(preg_match("/89\z/i", $string)) { // if our pattern matches we echo this echo 'The string ends with 89'; } else { // if no match is found we echo this line echo 'No match found'; } ?>[/CODE] 上面的脚本会输出: The string ends with 89 。到目前为止看上去都很简单^_ [FONT-COLOR=Red]备注: \Z 字符串结尾或行尾(不受处理多行选项的影响) \z 字符串结尾(不受处理多行选项的影响)[/FONT-COLOR]
// try to match our pattern if(preg_match("/^1\+1/i", $string)) { // if the pattern matches we echo this echo 'The string begins with 1+1'; } else { // if no match is found we echo this line echo 'No match found'; } ?> [/CODE] 上面这段脚本会输出: The string begins with 1+1 因为它发现了1+1而忽略了元字符+的特殊含义。如果你不对元字符进行转义而用如下的表达式 [CODE=php]preg_match("/^1+1/i", $string)[/CODE] 匹配将会失败。如果你想匹配\,那你同样要对其进行转义。但是我们同样需要要对控制字符进行转义,因为本身就是反斜线,所以需要进行两次转义: \\\\
// loop through the matches with foreach foreach($matches as $key=>$value) { echo $key.' -> '.$value; } ?>[/CODE] 上面的脚本会输出: 0 -> a 发生了什么呢?preg_match()函数发现第一个字符a和模式/[^b]/匹配。让我们对上面的代码做点小改变,用preg_match_all()去匹配字符串$string中的所有字符: [CODE=php] // create a string $string = 'abcefghijklmnopqrstuvwxyz0123456789';
// try to match all characters not within out pattern preg_match_all("/[^b]/", $string, $matches);
// loop through the matches with foreach foreach($matches[0] as $value) { echo $value; } ?> [/CODE] 上面的脚本将会输出字符串中除了字符b之外的所有字符: acefghijklmnopqrstuvwxyz0123456789 另外,我们还可以用它来过滤掉一个字符串中的数字: [CODE=php] // create a string $string = 'abcefghijklmnopqrstuvwxyz0123456789';
// match any character that is not a number between 0 and 9 preg_match_all("/[^0-9]/", $string, $matches);
// loop through the matches with foreach foreach($matches[0] as $value) { echo $value; } ?> [/CODE] 上面的代码发生了什么呢?我们用preg_match_all()函数去匹配我们的模式串,这个模式串用了元字符[FONT-COLOR=Red]^[/FONT-COLOR]和字符集[]去匹配除了0-9之外的所有字符。 因此,你可以简单的记忆元字符[FONT-COLOR=Red]^[/FONT-COLOR]用在字符集中表示的意思是非(NOT)。
八、还要跟着我继续学习吗?
好的,我们继续!我们来看看最常用的元字符:反斜线([FONT-COLOR=Red]\[/FONT-COLOR]) 在正则表达式中,反斜线([FONT-COLOR=Red]\[/FONT-COLOR])有几种不同的用法。第一个用法是用来进行转义,反斜线([FONT-COLOR=Red]\[/FONT-COLOR])可以用来转义所有元字符,包括其自身,因此你可以运用模式字符串来匹配它们。如果我们的字符串是这样的 "This is a [templateVar]" 我们想搜索[templateVar],我们可以这样来实现: [CODE=php] // create a string $string = 'This is a [templateVar]';
// try to match our pattern preg_match_all("/[\[\]]/", $string, $matches);
// loop through the matches with foreach foreach($matches[0] as $value) { echo $value; } ?> [/CODE] 上面的脚本会输出结果: [] 这是因为我们想匹配[]。如果没有反斜线,则模式字符串是这样[FONT-COLOR=Red]"/[[]]/"[/FONT-COLOR],但是我们必须对我们想要搜索的[FONT-COLOR=Red][[/FONT-COLOR]和[FONT-COLOR=Red]][/FONT-COLOR]进行转义。同样,你必须对反斜线也要进行转义。如果你有一个像这样的字符串: c:\dir\file.php 在模式字符串中你应该用\\。 反斜线还可以被用来表示特殊的字符序列。 接下来要介绍的元字符是点号([FONT-COLOR=Red].[/FONT-COLOR])。 点号可以用来匹配除换行(如\n、\r)之外的任意字符。因此我们可以用它来匹配除换行之外的任意字符。为了可以让点号[FONT-COLOR=Red].[/FONT-COLOR]可以匹配任意字符,包括换行,你可以使用/s修饰符。首先,我们来看看如何使用不带/s的点号[FONT-COLOR=Red].[/FONT-COLOR]来匹配任意字符: [CODE=php] // create a string $string = 'sex at noon taxes';
// look for a match echo preg_match("/s.x/", $string, $matches);
反斜线也被用在字符序列中。什么是字符序列呢? 字符序列就是那些预定义的字符集,你可以再类中使用它。 [CODE] \d - Matches any numeric character - same as [0-9]匹配任意数字字符 \D - Matches any non-numeric character - same as [^0-9]匹配任意非数字字符 \s - Matches any whitespace character - sames as [ \t\n\r\f\v]匹配任意空格字符 \S - Matches any non-whitespace character - same as [^ \t\n\r\f\v]匹配任意非空格字符 \w - Matches any alphanumeric character - same as [a-zA-Z0-9_]匹配任意字母数字字符 \W - Matches any non-alphanumeric character - same as [^a-zA-Z0-9_]匹配任意非字母数字字符 [/CODE] 运用上面的字符序列,我们可以再我们的模式字符串中使用,这样可以减少代码长度。看看下面的代码,你是否能说出输出结果: [CODE=php] // create a string $string = 'ab-ce*fg@ hi & jkl(mnopqr)stu+vw?x yz0>1234<567890';
// match our pattern containing a special sequence preg_match_all("/[\w]/", $string, $matches);
// loop through the matches with foreach foreach($matches[0] as $value) { echo $value; } ?> [/CODE] 我们来看看是如何运行的。我们用\w来匹配出任意字母数字字符,因此输出结果是: abcefghijklmnopqrstuvwxyz0123456789 这可以用来过滤用户名中的一些你不想要的非法字符。 运用同样的方法,我们可以一个字符串不以数字开头: [CODE=php] // create a string $string = '2 bad for perl';
// echo our string if(preg_match("/^\d/", $string)) { echo 'String begins with a number'; } else { echo 'String does not begin with a number'; } ?> [/CODE] [/FONT-SIZE]