向前查找指的是根据要查找的字符序列之后是否存在特定的字符序列(肯定式向前查找)或者不存在特定的字符序列(否定式向前查找)来确定是否匹配。向后查找则是指根据要查找的字符序列之前是否存在特定的字符(肯定式向后查找)或者不存在特定的字符序列(否定式向后查找)来确定是否匹配。一般来说,向前查找模式都位于要匹配模式的后面,而向后查找模式都位于要匹配模式的前面。如果改变向前查找和向后查找模式的位置,那么对正则表达式匹配过程会具有不同的影响。

我们拿下面的测试文本为例:

The current version of Microsoft SQL Server is SQL Server 2000. However a new version, SQL Server 2005 is scheduled for release for the calendar year 2005. The MySQL database product lacks some of the features of big commercial database products like SQL Server but the product team is working hard to provide an improved set of features.

注意其中包含 SQL Server 2000 和 SQL Server 2005。如果要查找 SQL Server 2000 中的 SQL Server,应该使用下面包含向前查找的模式:

SQL Server(?= 2000)

注意 (?= 2000) 中包含一个空格。该模式的含义是查找位于空格符和字符序列 2000 紧前面的字符序列 SQL Server。正则表达式引擎的工作原理是,首先匹配直接量字符序列 SQL Server,然后再从 Server 中的最后一个字符 r 开始向后匹配向前查找模式 (?= 2000)。如果该模式中的空格符和字符 2、0、0、0 依次都匹配,那么前面的 SQL Server 才会最终匹配;否则如果向前查找模式匹配失败,那么前面的 SQL Server 也不会匹配。

如果我们想查找测试文本中 like SQL Server 中的 SQL Server,那么就要使用下面包含向后查找的模式:

(?<=like )SQL Server

同样,要注意 (?<=like ) 中包含一个空格。这个模式的含义是查找紧跟在字符序列 like 和空格符后面的字符序列 SQL Server。正则表达式引擎的工作原理是,首先匹配直接量字符序列 SQL Sever,然后正则表达式引擎返回到 SQL Server 中首字母 S 之前的位置,依次匹配向后查找模式中的空格符和字符 e、k、i 和 l(是的,是反向匹配)。如果向后查找模式匹配成功,那么该模式后面的字符序列 SQL Server 才会匹配成功;否则,仍然被正则表达式引擎所忽略(匹配最终失败)。

前面使用的向前查找和向后查找模式都是处于正常的位置上--即向前查找位于要匹配的模式之后,向后查找位于要匹配的模式之前。而且,无论是在正则表达式中使用向前查找还是向后查找,正则表达式引擎都会首先尝试匹配 SQL Server,然后再匹配向前查找或向后查找组件--这一点很重要。那么,如果我们颠倒这种常规的位置,正则表达式引擎会怎么样来处理向前查找和向后查找呢?这正是我们最关心的问题。

首先,我们来变换向前查找的位置,将第一个正则表达式修改为:

(?= 2000)SQL Server

此时,正则表达式引擎的工作原理是,首先匹配直接字符序列 SQL Server,然后返回 SQL Server 中首字母 S 之前的位置,再尝试匹配向前查找模式。显然,向前查找模式中的第一个字符--空格符,不能与 SQL Server 中的第一个字符--大写的 S 匹配。因此,向前查找匹配失败,整个正则表达式模式也就匹配失败。当向前查找位于要匹配的模式之前时(比如当前的模式),向前查找中应该包含要匹配的模式,即需要写成下面这样:

(?=SQL Server 2000)SQL Server

在使用这个修正的模式后,正则表达式引擎的工作原理是,首先匹配直接量字符序列 SQL Server,然后正则表达式引擎返回到 SQL Server 中首字母 S 之前的位置开始尝试匹配向前查找模式。因为向前查找模式是 SQL Server 2000,所以其中的 SQL Server 一定会匹配,然后引擎又从 Server 中的最后一个 r 后面的位置开始测试空格符和直接量字符序列 2000,如果该组件也匹配,那么整个向前查找匹配成功,因而要查找的正则表达式模式匹配成功。由此可见,向前查找模式在正则表达式中的位置决定了正则表达式引擎在匹配了要匹配的模式(本例中是 SQL Server)之后,开始匹配向前查找模式的起始位置--即如果向前查找在后(正常情况下),则从匹配模式的字符序列的最后一个字符之后的位置开始;如果向前查找在前,则返回匹配模式的字符序列的第一个字符之前的位置开始。

下面我们再来验证一下修改向后查找的位置之后的情况。

经验证,如果我们使用正则表达式 SQL Server(?<=like ) 来匹配 like SQL Server 中的 SQL Server 不会成功。因为根据前面分析正则表达式引擎在匹配向后查找模式时是从后往前匹配的。所以,在使用以上模式时,正则表达式引擎的工作原理是,首先匹配直接量字符序列 SQL Server,由于向后查找模式在后面,所以引擎会从 Server 中最后一个字符 r 之后的位置开始向左依次匹配向后查找模式 like (包括后面的空格符)。当引擎首先尝试将模式中的空格符(最右边的字符)与 Server 中的 r 进行匹配时,匹配失败。由于向后查找模式匹配失败,所以整个正则表达式模式也匹配失败。当向后查找位于要匹配的模式之后时(比如当前的模式),向后查找中应该包含要匹配的模式,即需要写成下面这样:

SQL Server(?<=like SQL Server)

在使用这个修正的模式后,正则表达式引擎的工作原理是,首先匹配直接量字符序列 SQL Server,然后再从 Server 中最后一个 r 之后的位置开始向左尝试匹配向后查找模式(此时是 like SQL Server)。这样,引擎会尝试将向后查找中的 SQL Server 与一开始匹配的字符序列 SQL Server 从右往左再匹配一次,结果匹配。在到了 SQL Server 中第一个字符 S 之前的位置时,引擎又尝试匹配空格符和字符 e、k、i 和 l。如果向后查找模式匹配成功,则一开始匹配的 SQL Server 才会真正匹配;否则,引擎将会丢弃一开始匹配的 SQL Server,而从 Server 中的最后一个 r 之后的位置重新开始尝试匹配正则表达式。由此可见,向后查找模式在正则表达式中的位置决定了正则表达式引擎在匹配了要匹配的模式(本例中的 SQL Server)之后,开始匹配向后查找模式的起始位置--即如果向后查找在前(正常情况下),则返回匹配模式的字符序列的第一个字符之前的开始位置(反向匹配向后查找模式);如果向后查找在后,则从匹配模式的字符序列的最后一个字符开始(反向匹配向后查找模式)。

以上分析了向前查找和向后查找在正则表达式中的正常位置和颠倒位置对正则表达式引擎工作机制的不同影响。其中隐含地介绍了正则表达式引擎在匹配向前查找和向后查找模式时的关键性差别--即,匹配向前查找模式时,无论向前查找的位置在前在后,都是正向(从左往右)匹配;而匹配向后查找模式时,无论向后查找的位置在前在后,都是逆向(从右往左)匹配。而向前查找或向后查找模式的位置决定了开始匹配它们的起点--而这正是理解它们位置变化对匹配过程影响核心所在。

注:向前查找和向后查找的工作原理(或者说匹配次序)即使是对于从右往左书写的语言也是如此!



朋友们的留言

  1. UGG Boots Outlet | 11月 14th, 2011 at 08:56

    Thank you for sharing to us.Please one more post about that..This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your Blog is so perfect!linmei/comment201111

    Reply to this comment
  2. Pandora Jewellery | 11月 17th, 2011 at 09:55

    big thanks for shared.
    It is very nice site. Thanks for sharing nice information with us.
    Thanks for shaing This is great, nice post! very interesting and informative,
    Thanks for sharing this and I look forward to more great things in the future.linmei/comment201111

    Reply to this comment
  3. GHD New Wave Limited Edition | 11月 18th, 2011 at 15:00

    I agree with your blog, lucky to read your blog! It makes me have the courage to stick to it!linmei/comment201111

    Reply to this comment
  4. nfl uniform | 11月 19th, 2011 at 11:50

    Excellent task ! Your web-site has given me all the strategies and information I expected .linmei/comment201111

    Reply to this comment
  5. oakley sunglasses | 11月 21st, 2011 at 13:49

    Many articles is useful for me. I will be right back again.linmei/comment201111

    Reply to this comment
  6. hermes bags | 11月 22nd, 2011 at 15:53

    I simply added your web page to my favorites. I like reading your blog.linmei/comment201111

    Reply to this comment
  7. nfl football jerseys cheap | 11月 23rd, 2011 at 16:03

    This is one of the great blog post. I like your writing style. I appreciate your efforts. Keep posting some more interesting blog posts.linmei/comment201111

    Reply to this comment
  8. Pandora Jewellery | 11月 24th, 2011 at 17:06

    Thank u for your sharing.Your blog has a unique feature that can make the people who reads become happy.After reading your blog,I feel happy.

    linmei/comment201111

    Reply to this comment
  9. Canada Goose | 11月 26th, 2011 at 16:01

    Nice article , thank you for sharing your article. I genuinely enjoyed it. I place a url to my website to right here so other group men and girls can research it.linmei/comment201111

    Reply to this comment
  10. cheap uggs | 11月 28th, 2011 at 10:34

    Nice information and facts! I have been seeking for everything like this for some time currently. Many thanks!linmei/comment201111

    Reply to this comment
  11. Canada Goose | 11月 28th, 2011 at 17:07

    Nice article , thank you for sharing your article. I genuinely enjoyed it. I place a url to my website to right here so other group men and girls can research it.caiyifang/comment201111

    Reply to this comment
  12. UGG Bailey Button | 11月 30th, 2011 at 09:05

    Your blog has a unique feature that can make the people who reads become happy.After reading your blog,I feel happy

    Reply to this comment
  13. miu miu bags | 11月 30th, 2011 at 17:28

    Very detailed info. I am very glad to read this article. Thanks for giving us nice articles.linmei/comment201111

    Reply to this comment
  14. oakley glasses | 12月 2nd, 2011 at 10:13

    Thank you for sharing to us.Please one more post about that..This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your Blog is so perfect!linmei/comment201112

    Reply to this comment
  15. Pandora Jewellery | 12月 3rd, 2011 at 11:40

    big thanks for shared.
    It is very nice site. Thanks for sharing nice information with us.
    Thanks for shaing This is great, nice post! very interesting and informative,
    Thanks for sharing this and I look forward to more great things in the future.linmei/comment201112

    Reply to this comment
  16. Pandora Jewellery | 12月 5th, 2011 at 16:22

    Thank u for your sharing.Your blog has a unique feature that can make the people who reads become happy.After reading your blog,I feel happy.

    linmei/comment201112

    Reply to this comment
  17. New era hat | 12月 5th, 2011 at 17:12

    Monster Energy hats , although feet on the ground is watermelon, but I actually don’t know how to start.

    Reply to this comment
  18. cheap uggs | 12月 7th, 2011 at 09:18

    Nice information and facts! I have been seeking for everything like this for some time currently. Many thanks!linmei/comment201112

    Reply to this comment
  19. Christian Louboutin | 12月 8th, 2011 at 10:39

    Please keep continue about that.This is a great work done by you.Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!!!linmei/comment201112

    Reply to this comment
  20. ugg boots outlet | 12月 9th, 2011 at 13:53

    This blog is very cool!
    It is useful information.linmei/comment201112

    Reply to this comment
  21. Shop Cheap Moncler Jackets | 12月 9th, 2011 at 16:59

    Woot, I will cretialny put this to good use!

    Reply to this comment
  22. nfl uniform | 12月 12th, 2011 at 10:12

    Excellent task ! Your web-site has given me all the strategies and information I expected .linmei/comment201112

    Reply to this comment
  23. Canada goose outlet | 12月 13th, 2011 at 11:33

    This is a wonderful content. I will bookmark this site and visit again.linmei/comment201112

    Reply to this comment
  24. nfl uniform | 12月 21st, 2011 at 11:07

    Thanks for taking the time to chat about this, I feel fervently about this and I benefit from learning about this subject..caiyifang/comment201112

    Reply to this comment
  25. cheap newport cigarettes | 12月 21st, 2011 at 14:42

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

    Reply to this comment
  26. Moncler Coats | 12月 24th, 2011 at 09:57

    I found this is very useful and interesting blog.I noticed more suggestive information on your blog.Its really great for your creative thinking.Thanks for sharing.caiyifang/comment201112

    Reply to this comment
  27. Friday | 01月 5th, 2012 at 17:10

    Almost inevitably, this process leads to politicians having to field some uncomfortable questions from BBC interviewers.

    Reply to this comment
  28. chimney repairs | 01月 27th, 2012 at 04:38

    Excellent post , really good read

    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