Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Ruby | Regexp names() function

  • Last Updated : 11 Jun, 2021

Regexp#names() : names() is a Regexp class method which produces a formatted string-version of regular expression.
 

Syntax: Regexp.names()
Parameter: Regexp values
Return: a formatted string-version of regular expression

Example #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 =/(?<go>.)(?<for>.)(?<it>.)/
 
 
#  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"

Output : 
 



Regexp names form : []

Regexp names form : []

Regexp names form : ["go", "for", "it"]

Example #2 : 
 

Ruby




# Ruby code for Regexp.names() method
 
# declaring Regexp value
reg_a = /geeks/
 
# declaring Regexp value
reg_b = /(?<hi>.)(?<there>.)/
 
# declaring Regexp value
reg_c = /(?<i>.)(?<can>.)(?<code>.)/
 
 
#  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"

Output : 
 

Regexp names form : []

Regexp names form : ["hi", "there"]

Regexp names form : ["i", "can", "code"]

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :