使用regex匹配Ruby子字符串,并像索引一样引用它

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用regex匹配Ruby子字符串,并像索引一样引用它相关的知识,希望对你有一定的参考价值。

  1. # pass a Regexp into a String as if it were an Array index
  2.  
  3. "cat"[/c/]
  4. #=> "c"
  5. "cat"[/z/]
  6. #=> nil
  7.  
  8.  
  9. # pass in an optional second argument, to return the content of the nth matching group
  10.  
  11. re_phone = /(d{3})-(d{3})-(d{4})/
  12. "986-235-1001"[re_phone, 2]
  13. #=> "235"

以上是关于使用regex匹配Ruby子字符串,并像索引一样引用它的主要内容,如果未能解决你的问题,请参考以下文章