我正在使用 Ruby1.9.3。我是这个平台的新手。
从文档中,我刚刚熟悉了anchor
两个\z
和\G
。现在我稍微玩了一下\z
,看看它是如何工作的,因为定义(End或End of String)让我感到困惑,我无法理解它的意思 - by End
。所以我尝试了下面的小片段。但还是抓不住。
代码
irb(main):011:0> str = "Hit him on the head me 2\n" + "Hit him on the head wit>
=> "Hit him on the head me 2\nHit him on the head with a 24\n"
irb(main):012:0> str =~ /\d\z/
=> nil
irb(main):013:0> str = "Hit him on the head me 24 2\n" + "Hit him on the head >
=> "Hit him on the head me 24 2\nHit him on the head with a 24\n"
irb(main):014:0> str =~ /\d\z/
=> nil
irb(main):018:0> str = "Hit1 him on the head me 24 2\n" + "Hit him on the head>
=> "Hit1 him on the head me 24 2\nHit him on the head with a11 11 24\n"
irb(main):019:0> str =~ /\d\z/
=> nil
irb(main):020:0>
每次我得到nil
作为输出。那么计算是如何进行的\z
呢?是什么End
意思?- 我认为我的概念End
对文档中的单词有任何错误。因此,任何人都可以帮助我了解发生的原因以及为什么会发生这种情况?
而且我也没有找到任何锚的例子\G
。请你们提供任何示例来可视化如何\G
在实时编程中使用?
编辑
irb(main):029:0>
irb(main):030:0* ("{123}{45}{6789}").scan(/\G(?!^)\{\d+\}/)
=> []
irb(main):031:0> ('{123}{45}{6789}').scan(/\G(?!^)\{\d+\}/)
=> []
irb(main):032:0>
谢谢