Open In App

Ruby | Regexp =~() function

Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax: Regexp.=~()
Parameter: Regexp values
Return: true – if two regular expressions matches string otherwise return false

Example #1 :  

Ruby




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


Output : 

Regexp =~ form : 1

Regexp =~ form : 0

Regexp =~ form : 2

Example #2 :

Ruby




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


Output : 

Regexp =~ form : 0

Regexp =~ form : 

Regexp =~ form : 0

 


Last Updated : 04 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads