Ruby | Regexp compile() function

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 :



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Regexp compile form : (?-mix:a)

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

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Regexp compile form : (?-mix:geeks)

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

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

My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.