红宝石 | StringScanner 扫描函数
StringScanner#scan() : scan()是一个 StringScanner 类方法,它尝试与当前位置的模式匹配。
Syntax: StringScanner.scan()
Parameter: StringScanner values
Return: matched string otherwise return 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