这几天CSDN的通栏广告放着正则表达式培训的广告。正则表达式这玩意很有意思,在AS3中,也新增了正则表达式。正则表达式描述用于查找和处理字符串中的匹配文本的模式。正则表达式类似于字符串,但是可以包含特殊代码以描述模式和重复。切记,不同的编程环境实现正则表达式的方式也不同,所以,从别的地方看过来的正则表达式在AS3中未必合用。
具体的正则表达式语法规则请查看帮助文档。在网络上没看到过AS3正则表达式写的LRC歌词分析程序,下面,就给出一段使用正则表达式来分析LRC格式歌词的程序。
function
lrc(str:String):
void
... {
var results:Array;
results = str.match(/[ar:(.+)]/i);
var ar:String = results ? results[1] : '';
results = str.match(/[ti:(.+)]/i);
var ti:String = results ? results[1] : '';
results = str.match(/[al:(.+)]/i);
var al:String = results ? results[1] : '';
results = str.match(/[by:(.+)]/i);
var by:String = results ? results[1] : '';
results = str.match(/[offset:(.+)]/i);
var offset:int = results ? int(results[1]) : 0;
results = str.match(/[d+:.+][^[ ]*/ig);
if (!results)
...{
results = str.match(/[d+:.+][^[ ]*/ig);
}
if (!results)
...{
return;
}
var gc:Array = new Array();
for (var i:int = 0; i < results.length; i++)
...{
var ln:String = results[i];
var tm:String = ln.match(/[(.+)].*/)[1];
var wd:String = ln.match(/.+](.*)/)[1];
var ts:Array = tm.split('][');
for each (var t:String in ts)
...{
var c:Array = t.split(':');
gc.push(...{time:int((c[0]*60+c[1])*1000), word:wd});
}
}
gc.sortOn("time", Array.NUMERIC);
for each (var g:Object in gc)
...{
trace('时间:',g.time, '毫秒,歌词:', g.word);
}
}
... {
var results:Array;
results = str.match(/[ar:(.+)]/i);
var ar:String = results ? results[1] : '';
results = str.match(/[ti:(.+)]/i);
var ti:String = results ? results[1] : '';
results = str.match(/[al:(.+)]/i);
var al:String = results ? results[1] : '';
results = str.match(/[by:(.+)]/i);
var by:String = results ? results[1] : '';
results = str.match(/[offset:(.+)]/i);
var offset:int = results ? int(results[1]) : 0;
results = str.match(/[d+:.+][^[ ]*/ig);
if (!results)
...{
results = str.match(/[d+:.+][^[ ]*/ig);
}
if (!results)
...{
return;
}
var gc:Array = new Array();
for (var i:int = 0; i < results.length; i++)
...{
var ln:String = results[i];
var tm:String = ln.match(/[(.+)].*/)[1];
var wd:String = ln.match(/.+](.*)/)[1];
var ts:Array = tm.split('][');
for each (var t:String in ts)
...{
var c:Array = t.split(':');
gc.push(...{time:int((c[0]*60+c[1])*1000), word:wd});
}
}
gc.sortOn("time", Array.NUMERIC);
for each (var g:Object in gc)
...{
trace('时间:',g.time, '毫秒,歌词:', g.word);
}
}