zoukankan      html  css  js  c++  java
  • ruby 正则匹配返回值matchdata

    引用连接:

    为处理与正则表达式的匹配过程相关的信息而设置的类. 可以通过下列途径

    • Regexp.last_match
    • Regexp#match, String#match
    • $~

    得到该类的实例.

    超类:

    方法:

    self[n]

    返回第n个子字符串. 0表示整个匹配部分. 若n为负值,则从尾部算起(末尾的元素是第-1个). 若不存在第n个元素则返回nil.

    /(foo)(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.to_a       # => ["foobar", "foo", "bar", nil]
    p $~[0]         # => "foobar"
    p $~[1]         # => "foo"
    p $~[2]         # => "bar"
    p $~[3]         # => nil        (不匹配)
    p $~[4]         # => nil        (超出范围)
    p $~[-2]        # => "bar"
    
    self[start..end]

    Array#[start..end]相同。

    /(foo)(bar)/ =~ "foobarbaz"
    p $~[0..2]      # => ["foobar", "foo", "bar"]
    
    self[start, length]

    Array#[start, length]相同.

    /(foo)(bar)/ =~ "foobarbaz"
    p $~[0, 3]      # => ["foobar", "foo", "bar"]
    
    begin(n)

    返回第n个子字符串的首字符的偏移量(offset). 0表示整个匹配部分. 若n超出范围则引发IndexError异常. 若第n个子字符串没有匹配部分则返回nil.

    /(foo)(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.begin(0)   # => 0
    p $~.begin(1)   # => 0
    p $~.begin(2)   # => 3
    p $~.begin(3)   # => nil
    p $~.begin(-1)  # => `begin': index -1 out of matches (IndexError)
    
    end(n)

    返回第n个子字符串的尾字符的偏移量(offset). 0表示整个匹配部分. 若n超出范围则引发IndexError异常. 若第n个子字符串没有匹配部分则返回nil.

    /(foo)(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.end(0)   # => 6
    p $~.end(1)   # => 3
    p $~.end(2)   # => 6
    p $~.end(3)   # => nil
    p $~.end(-1)  # => `end': index -1 out of matches (IndexError)
    
    captures ((<ruby 1.8 特性>))

    返回一个包含$1,$2,...的数组. 与to_a不同的是,它不含$&. 若群组没有匹配部分,则相应的数组元素为nil.

    /(foo)(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.to_a       # => ["foobar", "foo", "bar", nil]
    p $~.captures       # => ["foo", "bar", nil]
    
    length
    size

    返回子字符串的数量(与self.to_a.size相同).

    /(foo)(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.size       # => 4
    
    offset(n)

    返回数组[start, end],它包含第n个子字符串的偏移量.它等同于

    [ self.begin(n), self.end(n) ]
    

    若第n个子字符串并未完成匹配则返回[nil, nil].

    post_match

    返回匹配部分之后的字符串(与$'相同).

    /(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.post_match # => "baz"
    
    pre_match

    返回匹配部分之前的字符串(与$`相同).

    /(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.pre_match  # => "foo"
    
    select { ... }

    ruby 1.8 特性

    与self.to_a.select { ... } 相同.

    string

    返回匹配对象的拷贝.返回的字符串已被冻结(Object#freeze).

    to_a

    返回包含$&,$1,$2,... 的数组。

    /(foo)(bar)(BAZ)?/ =~ "foobarbaz"
    p $~.to_a       # => ["foobar", "foo", "bar", nil]
    

    另外,请参考captures.

    to_s

    返回整个匹配部分.

    /bar/ =~ "foobarbaz"
    p $~            # => #<MatchData:0x401b1be4>
    p $~.to_s       # => "bar"
    
    values_at(index1, index2, ...)

    ruby 1.8 特性

    返回一个数组,它包括与正则表达式中第indexN个括号相匹配的子字符串. 与$&一样,第0个表示整个匹配部分.

    m = /(foo)(bar)(baz)/.match("foobarbaz")
    p m.values_at(0,1,2,3,4)   # same as m.to_a.values_at(...)
    p m.values_at(-1,-2,-3)
    
    => ["foobarbaz", "foo", "bar", "baz", nil]
       ["baz", "bar", "foo"]
    
  • 相关阅读:
    codechef BIBOARD
    Tree
    NOIWC2021总结
    星际穿越
    GDKOI总结
    lg4229
    P3320 [SDOI2015]寻宝游戏
    P6670 [清华集训2016] 汽水
    P6326 Shopping
    P3060 [USACO12NOV]Balanced Trees G
  • 原文地址:https://www.cnblogs.com/dami520/p/3216130.html
Copyright © 2011-2022 走看看