大家在ASP编程中是否会遇到以下问题呢?由于时间在字符串中的值不一致性,会出现以下几中格式的字符串,所以就没有办法用left,right,mid等函数直接来获取,通过资料查询和朋友的指点,想到了是否可以用正则表达式来实现这个功能,通过实践,最终解决了这个问题(你也还可以将这个功能改一下,就可以获取其他格式的字符串)。
<% ‘str = "2007-01-01 12:12:12 问题 1234" ‘str="sdfd 01-01 12:12:12 问题" ‘str="2007-01-01 12:12sdfdf" ‘str="qqwq 01-01 13:14dddd" str="a2007-01-01sdddd" patrn="(\d+[-\:\s])+\d+" Response.Write(RegExpTest(patrn,str)) Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ‘ 建立变量。 Set regEx = New RegExp ‘ 建立正则表达式。 regEx.Pattern = patrn ‘ 设置模式。 regEx.IgnoreCase = True ‘ 设置是否区分大小写。 regEx.Global = True ‘ 设置全局替换。 Set Matches = regEx.Execute(strng) ‘ 执行搜索。 For Each Match in Matches ‘ 遍历 Matches 集合。 RetStr = Match.Value Next RegExpTest = RetStr end Function %> |