Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Ruby | Regexp to_s() function

  • Last Updated : 17 Dec, 2019

Regexp#to_s() : to_s() is a Regexp class method which returns the string containing the regular expression with the same semantics.

Syntax: Regexp.to_s()

Parameter: Regexp values

Return: string containing the regular expression with the same semantics.

Example #1 :






# Ruby code for Regexp.to_s() method
   
# declaring Regexp value
reg_a = /a/
   
# declaring Regexp value
reg_b = /\xa1\xa2/e
   
#  to_s method
puts "Regexp to_s form : #{reg_a.to_s}\n\n"
   
puts "Regexp to_s form : #{reg_b.to_s}\n\n"

Output :

Regexp to_s form : (?-mix:a)

Regexp to_s form : (?-mix:\xa1\xa2)

Example #2 :




# Ruby code for Regexp.to_s() method
   
# declaring Regexp value
reg_a = /geeks/ix
   
# declaring Regexp value
reg_b = /(.)(.)/
   
#  to_s method
puts "Regexp to_s form : #{reg_a.to_s}\n\n"
   
puts "Regexp to_s form : #{reg_b.to_s}\n\n"
  

Output :

Regexp to_s form : (?ix-m:geeks)

Regexp to_s form : (?-mix:(.)(.))
My Personal Notes arrow_drop_up
Recommended Articles
Page :