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


Ruby StringScanner scan用法及代碼示例


StringScanner#scan():scan()是StringScanner類方法,嘗試與當前位置的圖案匹配。

用法:StringScanner.scan()

參數:StringScanner值


返回:匹配的字符串,否則返回false

示例1:

# Ruby code for StringScanner.scan() method 
  
# loading StringScanner 
require 'strscan'
  
# declaring StringScanner  
c = StringScanner.new("Mon Sep 12 2018 14:39") 
   
# scan() method 
c.scan(/\w+/) 
  
puts "String Scanner result of c.scan() form : #{c.pos()}\n\n"
  
# scan() method 
c.scan(/\s+/) 
  
puts "String Scanner result of c.scan() form : #{c.pos()}\n\n"

輸出:

String Scanner result of c.scan() form : 3

String Scanner result of c.scan() form : 4

示例2:

# Ruby code for StringScanner.scan() method 
  
# loading StringScanner 
require 'strscan'
  
# declaring StringScanner  
c = StringScanner.new("h ello geeks") 
   
# scan() method 
c.scan(/\w+/) 
  
puts "String Scanner result of c.scan() form : #{c.pos()}\n\n"
  
# scan() method 
c.scan(/\s+/) 
  
puts "String Scanner result of c.scan() form : #{c.pos()}\n\n"

輸出:

String Scanner result of c.scan() form : 1

String Scanner result of c.scan() form : 2


相關用法


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