当前位置: 移动技术网 > IT编程>开发语言>Asp > asp正则表达式使用详解

asp正则表达式使用详解

2017年12月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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
%>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网