📜  红宝石 |匹配数据类

📅  最后修改于: 2022-05-13 01:54:32.370000             🧑  作者: Mango

红宝石 |匹配数据类

在 Ruby 中,所有的模式匹配都是在特殊变量$~的帮助下完成的。所有模式匹配都会将$~设置为包含匹配信息的 MatchData。 MatchData对象由Regexp#matchRegexp.last_match方法返回。 MatchData对象包含模式匹配的所有结果,通常由特殊变量访问的结果,即$&、$'、$`、$1、$2 等

实例方法

这里match是 MatchData 类的对象。

  • [] :它被称为匹配参考。在这个 MatchData 中,它的行为类似于一个数组,可以使用普通的数组索引技术进行访问。在此 match[0] 等效于特殊变量 $& 并返回整个匹配的字符串。 match[1], match[2], match[3] 等等,返回匹配返回的参考值。
match[i]
match[start, length]
match[range]

例子:

Ruby
# Ruby program to illustrate
# use of []
 
# Using [] operator
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks12.")
a[0]
a[1, 4]
a[1..2]
a[-2, 1]


Ruby
# Ruby program to illustrate
# use of begin method
 
# Using begin method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.begin(1)
a.begin(2)


Ruby
# Ruby program to illustrate
# use of captures method
 
# Using captures method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.captures


Ruby
# Ruby program to illustrate
# use of end method
 
# Using end method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.end(0)
a.end(2)


Ruby
# Ruby program to illustrate
# use of length method
 
# Using length method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.length
 
# using size method
a.size


Ruby
# Ruby program to illustrate
# use of offset method
 
# Using offset method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.offset(2)
a.offset(1)


Ruby
# Ruby program to illustrate
# use of post_match method
 
# Using post_match method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.post_match


Ruby
# Ruby program to illustrate
# use of pre_match method
 
# Using pre_match method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.pre_match


Ruby
# Ruby program to illustrate
# use of string method
 
# Using string method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.string


Ruby
# Ruby program to illustrate
# use of to_a method
 
# Using to_a method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.to_a


Ruby
# Ruby program to illustrate
# use of to_s method
 
# Using to_s method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.to_s


Ruby
# Ruby program to illustrate
# use of values_at method
 
# Using values_at method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.values_at(2, 0)


输出:

"ks12"
["k", "s", "1", "2"]
["k", "s"]
["1"]
  • begin:此方法返回字符串中匹配数组的第 n 个元素的开始偏移量。
match.begin(n)

例子:

红宝石

# Ruby program to illustrate
# use of begin method
 
# Using begin method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.begin(1)
a.begin(2)

输出:

11
12
  • captures :此方法返回所有匹配组的数组。
match.captures

例子:

红宝石

# Ruby program to illustrate
# use of captures method
 
# Using captures method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.captures

输出:

["k", "s", "11", "2"]
  • end:此方法返回字符串中匹配数组的第 n 个元素末尾之后的字符的偏移量。
match.end(n)

例子:

红宝石

# Ruby program to illustrate
# use of end method
 
# Using end method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.end(0)
a.end(2)

输出:

16
13
  • length :此方法返回匹配数组中存在的元素数。
match.length

例子:

红宝石

# Ruby program to illustrate
# use of length method
 
# Using length method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.length
 
# using size method
a.size

输出:

5
5
  • offset :此方法返回一个二元素数组,其中包含第 n 个match的开始和结束偏移量。
match.offset(n)

例子:

红宝石

# Ruby program to illustrate
# use of offset method
 
# Using offset method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.offset(2)
a.offset(1)

输出:

[12, 13]
[11, 12]
  • post_match:此方法返回当前匹配后的原始字符串片段。与特殊变量 $' 相同。
match.post_match

例子:

红宝石

# Ruby program to illustrate
# use of post_match method
 
# Using post_match method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.post_match

输出:

": Ruby"
  • pre_match:此方法返回当前匹配之前的原始字符串片段。与特殊变量 $` 相同。
match.pre_match

例子:

红宝石

# Ruby program to illustrate
# use of pre_match method
 
# Using pre_match method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.pre_match

输出:

"GeeksFORgee"
  • select :此方法返回一个数组,该数组包含匹配块为真的所有元素。
match.select{|val|block}
  • size :此方法类似于MatchData#length方法。
match.size
  • 字符串 :此方法返回匹配中传递的字符串的冻结副本。
match.string

例子:

红宝石

# Ruby program to illustrate
# use of string method
 
# Using string method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.string

输出:

"GeeksFORgeeks112: Ruby"
  • to_a:此方法返回匹配数组。
match.to_a

例子:

红宝石

# Ruby program to illustrate
# use of to_a method
 
# Using to_a method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.to_a

输出:

["ks112", "k", "s", "11", "2"]
  • to_s:此方法返回整个匹配的字符串。
match.to_s

例子:

红宝石

# Ruby program to illustrate
# use of to_s method
 
# Using to_s method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.to_s

输出:

"ks112"
  • values_at:在此方法中,索引用于访问匹配的值并返回相应匹配的数组。
match.valu_at([index]*)

例子:

红宝石

# Ruby program to illustrate
# use of values_at method
 
# Using values_at method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.values_at(2, 0)

输出:

["s", "ks112"]
  • == :它被称为平等。它用于检查MatchData,即match1match2是否相等。如果它们相等,则返回 true,否则返回 false。
match1==match2

参考: https://ruby-doc.org/core-2.2.0/MatchData.html