Rubyの
String#scanscan.as
package com.blogspot.takumakei.utils
{
public function scan(str:String, re:RegExp):Array
{
if(!re.global){
var flags:String = 'g';
if(re.dotall)
flags += 's';
if(re.multiline)
flags += 'm';
if(re.ignoreCase)
flags += 'i';
if(re.extended)
flags += 'x';
re = new RegExp(re.source, flags);
}
var r:Array = [];
var m:Array = re.exec(str);
while(null != m){
if(1 == m.length)
r.push(m[0]);
else
r.push(m.slice(1, m.length));
m = re.exec(str);
}
return r;
}
}
サンプルスクリプト
p(scan('foobar', /./));
p(scan('foobar', /(.)/));
p(scan('foobarbazfoobarbaz', /ba./));
p(scan('foobarbazfoobarbaz', /(ba)(.)/));
実行結果
["f","o","o","b","a","r"]
[["f"],["o"],["o"],["b"],["a"],["r"]]
["bar","baz","bar","baz"]
[["ba","r"],["ba","z"],["ba","r"],["ba","z"]]
0 件のコメント:
コメントを投稿