Open In App

Ruby | Regexp source() function

Last Updated : 17 Dec, 2019
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
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 : Ruby
# Ruby code for Regexp.source() method

# declaring Regexp value
reg_a = /a/

# declaring Regexp value
reg_b = /\xa1\xa2/e

# declaring Regexp value
reg_c =/(?<go>.)(?<for>.)(?<it>.)/


#  source method
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 : Ruby
# Ruby code for Regexp.source() method

# declaring Regexp value
reg_a = /geeks/ix

# declaring Regexp value
reg_b = /(?<hi>.)(?<there>.)e/

# declaring Regexp value
reg_c = /(?<i>.)(?<can>.)(?<code>.)/


#  source method
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 : (?.)(?.)(?.)



Similar Reads