在正则表达式中,位置元字符又被称为“零宽度断言”,指的是匹配位置但不返回(或返回零宽度的)匹配内容的模式。 

^--匹配字符串(或多行模式下行)的开始位置;

$--匹配字符串(或多行模式行下)的结束位置;

\b--匹配位于 \w 字符和 \W 字符之间的位置(又称词边界)。

\B--匹配位于字符之间的位置(即非 \w 字符和 \W 字符之间的位置)。

\A--匹配一个字符串的开始位置。其行为不受多行模式的影响(PHP)。

\z--匹配一个字符串的结束位置。其行为不受多行模式的影响(PHP)。

对于测试字符串 look at this. 而言,在多行模式下,正则表达式 this$ 不会匹配其结尾处的 this,因为 $ 匹配行结尾的位置,而 this 中最后一个字符 s 之后并不是一个换行符,而是一个句点字符。若要匹配这个 this,必须使用正则表达式 this\b ,\b 匹配字母字符和非字母字符之间的位置,而 this 的最后一个字符 s 和其后面的句点字符之间的位置恰好是这样一个位置。

对于 \B 元字符,其用途如下:

如果你想匹配 golden 中的 old 就可以使用模式 \Bold\B ,这个模式匹配一个字符之间的位置后跟字符序列 o、l、d,后跟另一个字符之间的位置。也就是说,\B 不会匹配位于字符和非字符之间的位置,所以模式 \Bold\B 匹配的字符序列 old 一定是被包含在一个单词(或字符串)中间。 



朋友们的留言

  1. UGG Sheepskin Boots Australia | 09月 19th, 2011 at 09:12

    Your article is useful for me. It is a good article.

    Reply to this comment
  2. UGG Highkoo Sale | 09月 27th, 2011 at 11:32

    The blog article very surprised to me! Your writing is good.

    Reply to this comment
  3. Ed Hardy T Shirts | 10月 5th, 2011 at 15:37

    Who has deceiv'd thee so oft as thy self?

    Reply to this comment
  4. cheap nike shoes | 10月 14th, 2011 at 12:19

    Thanks for your sharing!

    Reply to this comment
  5. nike high dunks | 10月 29th, 2011 at 09:07

    Some sandals can also double as slippers or tods shoes sale, tods shoes sale.Some trainers may be used as running shoes or as tennis shoes;

    Reply to this comment

我来说两句儿

可以在留言中使用以下标签 :<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree