Ruby Regexp =~()函数
Regexp#=~() : =~() 是一个Regexp类方法,它可以将一个正则表达式与一个字符串相匹配。
语法: Regexp.=~()
参数: Regexp值
返回: 真 – 如果两个正则表达式匹配字符串,否则返回假。
例子 #1 :
# 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"
输出:
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