Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Regexp new() function

  • Last Updated : 17 Dec, 2019

Regexp#new() : new() is a Regexp class method which returns a new regular expression pattern.

Syntax: Regexp.new()

Parameter: Regexp values

Return: a new regular expression pattern

Example #1 :






# Ruby code for Regexp.new() method
   
# declaring Regexp value
reg_a = Regexp.new('/a/')
   
 # declaring Regexp value
reg_c = Regexp.new('\*?{}.')
    
#  new method
puts "Regexp new form : #{reg_a}\n\n"
   
puts "Regexp new form : #{reg_c}\n\n"

Output :

Regexp new form : (?-mix:\/a\/)

Regexp new form : (?-mix:\*?{}.)

Example #2 :




# Ruby code for Regexp.new() method
  
# declaring Regexp value
reg_a = Regexp.new('/geeks/')
  
# declaring Regexp value
reg_b = Regexp.new('/(?<geeks>.)(?<for>.)(?<geeks>.)/')
  
# declaring Regexp value
reg_c = Regexp.new('\*?????{}.')
  
  
#  new method
puts "Regexp new form : #{reg_a}\n\n"
  
puts "Regexp new form : #{reg_b}\n\n"
  
puts "Regexp new form : #{reg_c}\n\n"

Output :

Regexp new form : (?-mix:\/geeks\/)

Regexp new form : (?-mix:\/(?.)(?.)(?.)\/)

Regexp new form : (?-mix:\*?????{}.)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!