📜  红宝石 |正则表达式名称()函数

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

红宝石 |正则表达式名称()函数

Regexp#names() : names()是一个 Regexp 类方法,它产生一个格式化的字符串版本的正则表达式。

示例 #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"]