Open In App

Ruby | Regexp source() function

Improve
Improve
Like Article
Like
Save
Share
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 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 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 : (?.)(?.)(?.)



Last Updated : 17 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads