Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Regexp compile() function

  • Last Updated : 17 Dec, 2019
Geek Week

Regexp#compile() : compile() is a Regexp class method which completes a new Regular Expression.

Syntax: Regexp.compile()

Parameter: Regexp values

Return: creates a new Regular Expression.

Example #1 :






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

Output :

Regexp compile form : (?-mix:a)

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

Example #2 :




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

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :