Ruby | Regexp names() function

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

Syntax: Regexp.names()

Parameter: Regexp values



Return: a formatted string-version of regualar expression

Example #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

Regexp names form : []

Regexp names form : []

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

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

Regexp names form : []

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

Regexp names form : ["i", "can", "code"]
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.