红宝石 |正则表达式 match()函数
Regexp#match() : force_encoding?()是一个 Regexp 类方法,它将正则表达式与字符串匹配,并指定字符串中开始搜索的位置。
Syntax: Regexp.match()
Parameter: Regexp values
Return: regular expression with the string after matching it.
示例 #1:
# Ruby code for Regexp.match() method
# declaring Regexp value
reg_a = /a/
# declaring Regexp value
reg_b = /geeks/
# declaring Regexp value
reg_c = /a/
# match method
puts "Regexp match form : #{reg_a.match("abcd")}\n\n"
puts "Regexp match form : #{reg_b.match("geeksforgeeks")}\n\n"
puts "Regexp match form : #{reg_c.match("playway")}\n\n"
输出 :
Regexp match form : a
Regexp match form : geeks
Regexp match form : a
示例 #2:
# Ruby code for Regexp.match() method
# declaring Regexp value
reg_a = /geeks/
# declaring Regexp value
reg_b = /problem/
# declaring Regexp value
reg_c = /code/
# match method
puts "Regexp match form : #{reg_a.match("geeksforgeeks")}\n\n"
puts "Regexp match form : #{reg_b.match("geeksforgeeks")}\n\n"
puts "Regexp match form : #{reg_c.match("codeer")}\n\n"
输出 :
Regexp match form : geeks
Regexp match form :
Regexp match form : code