asp正则表达式使用详解

代码如下:

Dim re 
Set re = new RegExp '创建RegExp实例 
re.Pattern = "ab+c" '定义正则表达式文字,你可以在这里替换正则表达式 
Dim myString 
myString = "abcefg" '定义要匹配的字符串,可以进行替换 
Response.write(re.Execute(myString)(0)) '进行匹配测试,并写出结果

代码如下:

<% 
Dim re 
Set re = new RegExp '创建RegExp实例 
re.Pattern = "\w+" '定义正则表达式文字 
Dim myString 
myString = "VBScript version 5.6 provides many new features." 
'进行匹配测试,并写出是否匹配成功 
If re.Test(myString) Then 
Response.write("匹配成功!") 
Else 
Response.write("匹配不成功!") 
End If 
%>

代码如下:

<% 
Dim re 
Set re = new RegExp '创建RegExp实例 
re.Pattern = "\s" '定义正则表达式文字,这里是匹配空白 
Dim myString 
myString = "VBScript version 5.6 provides many new features." 
myString = re.replace(myString, "-") '用-替换空白,返回替换后的字符串 
'写出结果 
Response.write(myString) 
%>

代码如下:

<% 
Dim re 
Set re = new RegExp '创建RegExp实例 
re.Global = True 
re.Pattern = "\s" '定义正则表达式文字,这里是匹配空白 
Dim myString 
myString = "VBScript version 5.6 provides many new features." 
myString = re.replace(myString, "-") '用-替换空白,返回替换后的字符串 
'写出结果 
Response.write(myString) 
%>

代码如下:

<% 
Dim re 
Set re = new RegExp '创建RegExp实例 
re.Global = True 
re.Pattern = "(\w+)-(\w+)" '定义正则表达式模式文字 
Dim myString 
myString = "flip-flop" 
myString = re.replace(myString, "$1-$2") 
'$1表示第一个\w+,$2表示第二个\w+,第一个\w+匹配flip,第二个\w+匹配flop, 
'所以$1-$2相当于flip-flop 
'写出结果 
Response.write(myString) 
%>

代码如下:

<% 
Dim re 
Set re = new RegExp '创建RegExp实例 
re.Global = True 
re.Pattern = "(\S+)(\s+)(\S+)" '定义正则表达式模式文字 
Dim myString 
myString = "flip flop" 
myString = re.replace(myString, "$3$2$1") 
'$1表示第一个\S+,$3表示第二个\S+,$2表示\s+, 
'所以$3$2$1相当于flop flip 
'写出结果 
Response.write(myString) 
%>

代码如下:

<% 
Dim re 
Set re = new RegExp '创建RegExp实例 
re.Global = True 
re.Pattern = "\w+" '定义正则表达式模式文字 
Dim myString 
myString = "VBScript version 5.6 provides many new features." 
Set Matches = re.Execute(myString) '执行搜索,该集合用来保存匹配的结果 
'进行匹配测试,并写出结果 
'迭代Matches集合 
For Each Match in Matches 
'写出结果 
Response.write(Match.FirstIndex & "-" & (Match.FirstIndex + Match.Length) & " " & Match.Value & "<br />") 
Next 
%>

代码如下:

<% 
Dim re 
Set re = new RegExp '创建RegExp实例 
're.Global = True 注释掉这一行 
re.Pattern = "\w+" '定义正则表达式模式文字 
Dim myString 
myString = "VBScript version 5.6 provides many new features." 
Set Matches = re.Execute(myString) '执行搜索,该集合用来保存匹配的结果 
'进行匹配测试,并写出结果 
'迭代Matches集合 
For Each Match in Matches 
'写出结果 
Response.write(Match.FirstIndex & "-" & (Match.FirstIndex + Match.Length) & " " & Match.Value & "<br />") 
Next 
%>

代码如下:

<%@language="vbscript" codepage="65001"%> 
<% 
'创建一个连接,并且创建一个ADODB.Command用于操作 
Dim oCmd,oConn 
Set oConn = Server.CreateObject("ADODB.Connection") 
Set oCmd = Server.CreateObject("ADODB.Command") 
oConn.ConnectionString = "Provider=SQLOLEDB;server = myhost;Initial Catalog = myDatabase;UID=sa;PWD=verysecret;" 
oConn.Open 
'这里创建一个SQL CREATE TABLE语句 
Set oCmd.ActiveConnection = oConn 
oCmd.CommandText = "CREATE TABLE NewEmployees(firstName nvarchar (50),lastName nvarchar (50),EmpType nvarchar (50))" 
'执行创建数据表操作 
oCmd.Execute 
Response.Write("操作成功!") 
%> 
<% 
'显式的关闭连接 
oConn.Close 
Set oConn = Nothing 
%>

相关推荐