當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Scala Regexp =~()用法及代碼示例


Regexp#=~():=~() 是 Regexp 類方法,它將正則表達式與字符串進行匹配。

用法:Regexp.=~()
參數: Regexp values
Return: true - if two regular expressions matches string otherwise return false

示例#1:

紅 gems


# 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:

紅 gems


# 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

相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Regexp =~() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。