日期:2014-05-17  浏览次数:20956 次

获取时间字符串
<%
str= "2007-01-01   12:12:12   问题   1234 "  
'str= "sdfd   2007-01-01   12:12:12   问题 "  
'str= "2007-01-01   12:12sdfdf "  
'str= "a   2007-01-01   sdddd "
%>

在asp中,如何用正则表达式来获取以上字符串是的时间字符串?

------解决方案--------------------
str = "2007-01-01 12:12:12 问题 1234 "
patrn = "\d{4}[-][0,1]{1}\d{1}[-][0-3]{1}\d{1} "

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 = RetStr & "Match " & I & " found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is ' "
RetStr = RetStr & Match.Value & " '. " & vbCRLF
Next
RegExpTest = RetStr
End Function


Response.Write(RegExpTest( "\d{4}[-][0,1]{1}\d{1}[-][0-3]{1}\d{1} ",str))