红宝石 |正则表达式 =~()函数
Regexp#=~() : =~()是一个 Regexp 类方法,它将正则表达式与字符串。
Syntax: Regexp.=~()
Parameter: Regexp values
Return: true – if two regular expressions matches string otherwise return false
示例 #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"
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"
输出 :
Regexp =~ form : 1
Regexp =~ form : 0
Regexp =~ form : 2
示例 #2:
红宝石
# 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"
输出 :
Regexp =~ form : 0
Regexp =~ form :
Regexp =~ form : 0