免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3079 | 回复: 3
打印 上一主题 下一主题

ruby 的回溯 正则表达式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-05 20:14 |只看该作者 |倒序浏览
ruby 的回溯 正则表达式





matchesrubyregular expression
Ruby代码
  1. ^ - Matches beginning of line   
  2. $ - Matches end of line   
  3. \A - Matches beginning of string.   
  4. \Z - Matches end of string. If string ends with a newline, it matches just before newline   
  5. \z - Matches end of string   
  6. \G - Matches point where last match finished   
  7. \b - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets   
  8. \B - Matches non-word boundaries   
  9. (?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text   
  10. (?!pat) - Negative lookahead assertion: ensures that the following characters do not match pat, but doesn't include those characters in the matched text   
  11. (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text   
  12. (?<!pat) - Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn't include those characters in the matched text   
  13. # If a pattern isn't anchored it can begin at any point in the string   
  14.   
  15. /real/.match("surrealist") #=> #<MatchData "real">   
  16. # Anchoring the pattern to the beginning of the string forces the   
  17. # match to start there. 'real' doesn't occur at the beginning of the   
  18. # string, so now the match fails   
  19. /\Areal/.match("surrealist") #=> nil   
  20. # The match below fails because although 'Demand' contains 'and', the   
  21. pattern does not occur at a word boundary.   
  22. /\band/.match("Demand")   
  23. # Whereas in the following example 'and' has been anchored to a   
  24. # non-word boundary so instead of matching the first 'and' it matches   
  25. # from the fourth letter of 'demand' instead   
  26. /\Band.+/.match("Supply and demand curve") #=> #<MatchData "and curve">   
  27. # The pattern below uses positive lookahead and positive lookbehind to   
  28. # match text appearing in <b></b> tags without including the tags in the   
  29. # match   
  30. /(?<=<b>)\w+(?=<\/b>)/.match("Fortune favours the <b>bold</b>")   
  31.     #=> #<MatchData "bold">  


  32. ^ - Matches beginning of line
  33. $ - Matches end of line
  34. \A - Matches beginning of string.
  35. \Z - Matches end of string. If string ends with a newline, it matches just before newline
  36. \z - Matches end of string
  37. \G - Matches point where last match finished
  38. \b - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets
  39. \B - Matches non-word boundaries
  40. (?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text
  41. (?!pat) - Negative lookahead assertion: ensures that the following characters do not match pat, but doesn't include those characters in the matched text
  42. (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text
  43. (?<!pat) - Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn't include those characters in the matched text
  44. # If a pattern isn't anchored it can begin at any point in the string

  45. /real/.match("surrealist") #=> #<MatchData "real">
  46. # Anchoring the pattern to the beginning of the string forces the
  47. # match to start there. 'real' doesn't occur at the beginning of the
  48. # string, so now the match fails
  49. /\Areal/.match("surrealist") #=> nil
  50. # The match below fails because although 'Demand' contains 'and', the
  51. pattern does not occur at a word boundary.
  52. /\band/.match("Demand")
  53. # Whereas in the following example 'and' has been anchored to a
  54. # non-word boundary so instead of matching the first 'and' it matches
  55. # from the fourth letter of 'demand' instead
  56. /\Band.+/.match("Supply and demand curve") #=> #<MatchData "and curve">
  57. # The pattern below uses positive lookahead and positive lookbehind to
  58. # match text appearing in <b></b> tags without including the tags in the
  59. # match
  60. /(?<=<b>)\w+(?=<\/b>)/.match("Fortune favours the <b>bold</b>")
  61.     #=> #<MatchData "bold">
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-05 20:43 |只看该作者
谢谢分享

论坛徽章:
3
寅虎
日期:2013-11-27 07:53:29申猴
日期:2014-09-12 09:24:152015年迎新春徽章
日期:2015-03-04 09:48:31
3 [报告]
发表于 2012-03-06 09:03 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
3
寅虎
日期:2013-11-27 07:53:29申猴
日期:2014-09-12 09:24:152015年迎新春徽章
日期:2015-03-04 09:48:31
4 [报告]
发表于 2012-03-06 09:15 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP