- ·上一篇文章:截字符串,汉字一个算两个字符,英文算一个字符
- ·下一篇文章:asp无组件文件上传
asp中获取字符串中的时间字符串
由于时间在字符串中的值不一致性,会出现以下几中格式的字符串,没有办法用left,right,mid直接来获取,所以想到用正则表达式来实现这个功能(只要将这个功能改一下,就可以获取其他格式的字符串。
<
%
&
#39
str 
= 
"
2007-01-01 
12:12:12 
问题 
1234"
&
#39
str="
sdfd 
01-01 
12:12:12 
问题"
 
&
#39
str="
2007-01-01 
12:12sdfdf"
 
&
#39
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 
 
 
&
#39
 
建立变量。
Set 
regEx 
= 
New 
RegExp 
 
 
&
#39
 
建立正则表达式。
regEx.Pattern 
= 
patrn 
 
 
&
#39
 
设置模式。
regEx.IgnoreCase 
= 
True 
 
 
&
#39
 
设置是否区分大小写。
regEx.Global 
= 
True 
 
 
&
#39
 
设置全局替换。
Set 
Matches 
= 
regEx.Execute(strng) 
 
 
&
#39
 
执行搜索。
For 
Each 
Match 
in 
Matches 
 
 
&
#39
 
遍历 
Matches 
集合。
RetStr 
= 
Match.Value
Next
RegExpTest 
= 
RetStr
end 
Function
%>
<
%
&
#39
str 
= 
"
2007-01-01 
12:12:12 
问题 
1234"
&
#39
str="
sdfd 
01-01 
12:12:12 
问题"
 
&
#39
str="
2007-01-01 
12:12sdfdf"
 
&
#39
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 
 
 
&
#39
 
建立变量。
Set 
regEx 
= 
New 
RegExp 
 
 
&
#39
 
建立正则表达式。
regEx.Pattern 
= 
patrn 
 
 
&
#39
 
设置模式。
regEx.IgnoreCase 
= 
True 
 
 
&
#39
 
设置是否区分大小写。
regEx.Global 
= 
True 
 
 
&
#39
 
设置全局替换。
Set 
Matches 
= 
regEx.Execute(strng) 
 
 
&
#39
 
执行搜索。
For 
Each 
Match 
in 
Matches 
 
 
&
#39
 
遍历 
Matches 
集合。
RetStr 
= 
Match.Value
Next
RegExpTest 
= 
RetStr
end 
Function
%>
asp中获取字符串中的时间字符串