Ruby StringScanner scan功能

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程