
108
|
第
4
章
字符串
表达式定位到字符的结尾)。
preg_match("/cow$/", "Dave was a cowhand"); //
返回
false
preg_match("/cow$/", "Don't have a cow"); //
返回
true
在正则表达式中,句点(
.
)可以匹配任意单个字符 :
preg_match("/c.t/", "cat"); //
返回
true
preg_match("/c.t/", "cut"); //
返回
true
preg_match("/c.t/", "c t"); //
返回
true
preg_match("/c.t/", "bat"); //
返回
false
preg_match("/c.t/", "ct"); //
返回
false
如果你想匹配一个特殊字符(称为
元字符
),需要使用反斜杠对其进行转义 :
preg_match("/\$5\.00/","Your bill is $5.00 exactly"); //
返回
true
preg_match("/$5.00/", "Your bill is $5.00 exactly"); //
返回
false
正则表达式默认是区分大小写的,所以
"/cow/"
不能匹配到
"COW"
。如果想执行一个不
区分大小写的匹配,需要指定一个特定的标签(在后面的章节中会看到)。
到目前为止,我们还没有做任何字符串函数(例如,
strstr()
)无法处理的事情,那为
什么需要正则表达式呢
?
正则表达式的强大来自它可以指定一个抽象模式,而去匹配许 ...