红宝石 |正则表达式名称()函数
Regexp#names() : names()是一个 Regexp 类方法,它产生一个格式化的字符串版本的正则表达式。
Syntax: Regexp.names()
Parameter: Regexp values
Return: a formatted string-version of regular expression
示例 #1:
Ruby
# Ruby code for Regexp.names() method
# declaring Regexp value
reg_a = /a/
# declaring Regexp value
reg_b = /geeks/
# declaring Regexp value
reg_c =/(?.)(?.)(?.)/
# names method
puts "Regexp names form : #{reg_a.names}\n\n"
puts "Regexp names form : #{reg_b.names}\n\n"
puts "Regexp names form : #{reg_c.names}\n\n"
Ruby
# Ruby code for Regexp.names() method
# declaring Regexp value
reg_a = /geeks/
# declaring Regexp value
reg_b = /(?.)(?.)/
# declaring Regexp value
reg_c = /(?.)(?.)(?.)/
# names method
puts "Regexp names form : #{reg_a.names}\n\n"
puts "Regexp names form : #{reg_b.names}\n\n"
puts "Regexp names form : #{reg_c.names}\n\n"
输出 :
Regexp names form : []
Regexp names form : []
Regexp names form : ["go", "for", "it"]
示例 #2:
红宝石
# Ruby code for Regexp.names() method
# declaring Regexp value
reg_a = /geeks/
# declaring Regexp value
reg_b = /(?.)(?.)/
# declaring Regexp value
reg_c = /(?.)(?.)(?.)/
# names method
puts "Regexp names form : #{reg_a.names}\n\n"
puts "Regexp names form : #{reg_b.names}\n\n"
puts "Regexp names form : #{reg_c.names}\n\n"
输出 :
Regexp names form : []
Regexp names form : ["hi", "there"]
Regexp names form : ["i", "can", "code"]