孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

正则表达式的分组

在模式内部,\1序列指的是第一个组的匹配,\2序列指的是第二个组的匹配,如

irb(main):008:0> "12:50am"=~/(\d\d):(\d\d)(..)/
=> 0
irb(main):009:0> "Hour is #$1,minute is #$2"
=> "Hour is 12,minute is 50"
irb(main):010:0> "12:50am"=~/((\d\d):(\d\d))(..)/
=> 0
irb(main):011:0> "Time is #$1"
=> "Time is 12:50"
irb(main):012:0> "Hour is #$2,minute is #$3"
=> "Hour is 12,minute is 50"
irb(main):013:0> "AM/PM is #$4"
=> "AM/PM is am"
irb(main):014:0>

posted on 2012-03-23 20:26  孤独的猫  阅读(177)  评论(0编辑  收藏  举报