Open In App

Ruby | Regexp new() function

Last Updated : 17 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:\*?????{}.)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads