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