📜  红宝石 |正则表达式 =~()函数

📅  最后修改于: 2022-05-13 01:55:06.520000             🧑  作者: Mango

红宝石 |正则表达式 =~()函数

Regexp#=~() : =~()是一个 Regexp 类方法,它将正则表达式与字符串。

示例 #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