红宝石 |正则表达式 named_captures()函数
Regexp#named_captures() :named_captures()是一个 Regexp 类方法,它返回一个散列,表示有关正则表达式的命名捕获的信息。散列的键是命名捕获的名称,生成正则表达式的格式化字符串版本。
Syntax: Regexp.named_captures()
Parameter: Regexp values
Return: a hash representing information about named captures of regular expression
示例 #1:
# Ruby code for Regexp.named_captures() method
# declaring Regexp value
reg_a = /a/
# declaring Regexp value
reg_b = /geeks/
# declaring Regexp value
reg_c = /a/
# named_captures method
puts "Regexp named_captures form : #{reg_a.named_captures}\n\n"
puts "Regexp named_captures form : #{reg_b.named_captures}\n\n"
puts "Regexp named_captures form : #{reg_c.named_captures}\n\n"
输出 :
Regexp named_captures form : {}
Regexp named_captures form : {}
Regexp named_captures form : {}
示例 #2:
# Ruby code for Regexp.named_captures() method
# declaring Regexp value
reg_a = /geeks/
# declaring Regexp value
reg_b = /problem/
# declaring Regexp value
reg_c = /code/
# named_captures method
puts "Regexp named_captures form : #{reg_a.named_captures}\n\n"
puts "Regexp named_captures form : #{reg_b.named_captures}\n\n"
puts "Regexp named_captures form : #{/(?.)(?.)/.named_captures}\n\n"
输出 :
Regexp named_captures form : {}
Regexp named_captures form : {}
Regexp named_captures form : {"goio"=>[1, 2]}