Ruby | Regexp source() function
Regexp#source() : source() is a Regexp class method which returns the original string of the pattern.
Syntax: Regexp.source()
Parameter: Regexp values
Return: original string of the pattern.
Example #1 :
reg_a = /a/
reg_b = /\xa1\xa2/e
reg_c =/(?<go>.)(?< for >.)(?<it>.)/
puts "Regexp source form : #{reg_a.source}\n\n"
puts "Regexp source form : #{reg_b.source}\n\n"
puts "Regexp source form : #{reg_c.source}\n\n"
|
Output :
Regexp source form : a
Regexp source form : \xa1\xa2
Regexp source form : (?.)(?.)(?.)
Example #2 :
reg_a = /geeks/ix
reg_b = /(?<hi>.)(?<there>.)e/
reg_c = /(?<i>.)(?<can>.)(?<code>.)/
puts "Regexp source form : #{reg_a.source}\n\n"
puts "Regexp source form : #{reg_b.source}\n\n"
puts "Regexp source form : #{reg_c.source}\n\n"
|
Output :
Regexp source form : geeks
Regexp source form : (?.)(?.)e
Regexp source form : (?.)(?.)(?.)
Last Updated :
17 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...