Related Articles

Related Articles

Ruby | Regexp source() function
  • Last Updated : 17 Dec, 2019

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 :



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Regexp source form : a

Regexp source form : \xa1\xa2

Regexp source form : (?.)(?.)(?.)

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Regexp source form : geeks

Regexp source form : (?.)(?.)e

Regexp source form : (?.)(?.)(?.)


My Personal Notes arrow_drop_up
Recommended Articles
Page :