Ruby | Regexp =~() function
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
reg_a = /a/
reg_b = /geeks/
reg_c = /a/
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
reg_a = /geeks/
reg_b = /problem/
reg_c = /code/
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
Share your thoughts in the comments
Please Login to comment...