Related Articles

Related Articles

Ruby | Regexp =~() function
  • Last Updated : 17 Dec, 2019

Regexp#=~() : =~() is a Regexp class method which matches a rgular expression against a string.

Syntax: Regexp.=~()

Parameter: Regexp values

Return: true – if two regular expressions matches string otherwise return false

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for Regexp.=~() method
  
# declaring Regexp value
reg_a = /a/
  
# declaring Regexp value
reg_b = /geeks/
  
# declaring Regexp value
reg_c = /a/
  
  
#  =~ method
puts "Regexp =~ form : #{reg_a =~ "happy"}\n\n"
  
puts "Regexp =~ form : #{reg_b =~ "geeksforgeeks"}\n\n"
  
puts "Regexp =~ form : #{reg_c =~ "goal"}\n\n"

chevron_right


Output :

Regexp =~ form : 1

Regexp =~ form : 0

Regexp =~ form : 2

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for Regexp.=~() method
  
# declaring Regexp value
reg_a = /geeks/
  
# declaring Regexp value
reg_b = /problem/
  
# declaring Regexp value
reg_c = /code/
  
  
#  =~ method
puts "Regexp =~ form : #{reg_a =~ "geeksforgeeks"}\n\n"
  
puts "Regexp =~ form : #{reg_b =~ "property"}\n\n"
  
puts "Regexp =~ form : #{reg_c =~ "codemonk"}\n\n"

chevron_right


Output :

Regexp =~ form : 0

Regexp =~ form : 

Regexp =~ form : 0

My Personal Notes arrow_drop_up
Recommended Articles
Page :