<%
if request("infoid")<>"" then
set rs=conn.execute("select * from nproduct where id="&request("infoid"))
if not (rs.eof and rs.bof) then
proname=rs("proname")
content=rs("proinfo")
end if
rs.close
set rs=nothing
end if
%>
上面我介绍了有关正则表达式的基本概念,有关它的详细说明和语法可以查阅MSDN和 JavaScript中的有关帮助。 在VB中如果你想引用正则表达式的话,你需要在项目中引用"Microsoft VBScript Regular Expressions"。但是如果你要是使用脚本的话就不必要了,因为在脚本里面这已经是一个 内在的对象供你引用了。 当然你需要在你的机器上安装IE4以上罗。 这个对象(在JavaScript中)叫RegExp 下面还是让代码来说明问题把,现在假设你想查看一个文挡里面是否包含一个特定的 字符串(例如"regular expressions") 代码见下: 代码使用VB写成。 Public Function IsTermInDocument(filePath As String,_ expr As String) As Boolean Dim fs As FileSystemObject Dim ts As TextStream Dim re As RegExp Dim text As String
Set fs = New FileSystemObject Set ts = fs.OpenTextFile(filePath, ForReading) text = ts.ReadAll Set re = New RegExp re.Pattern = expr IsTermInDocument = re.Test(text) End Function
Public Function Replacex(sourceStr as String, oldStr as _ String, newStr as String, optional ignoreCase as _ Boolean = False,optional isGlobal as Boolean = True) Dim re As New RegExp re.Pattern = oldStr re.Global = isGlobal re.IgnoreCase = ignoreCase Replacex = re.Replace(sourceStr, newStr) End Function
下面给出使用它的一些例子: Debug.Print Replacex("This is a test","is","at") --> "That at a test" 最精彩的还是使用正则表达式了 Debug.Print Replacex("This is a test","\ws","at") --> "That at a tatt" 甚至还可以这样 Debug.Print Replacex("This is a test","(\ws)","at$1") --> "Thatis atis a tatist"
获得XML的节点 现在,我们将一起来看看正则表达式在XML中是如何运用的。 首先,在Microsoft's XML 2.0解释器里面有两大难题: 第一, 这个XML解释器在装载XML文挡的时候必须要保证入口 满足定义在DTD的范围之内。这是一个大麻烦,因为就目前的情况来看, XML更改频繁,不时会多出一些标准,不时又会产生新的标志。 第二,如果使用XSL的话不能够操纵DTD,甚至当你使用脚本语言也是一件很费力的事情。 经常要做的是你需要在XSL使用XSL的结构表达式中设置一些变量,例如 浏览器的类型或则ASP的参数呀 这时你可以使用正则表达式来解决这些问题。 当你想获取一个XML元素的时候,也许这个对象有可能并不是你想要的东东。 例如:假设一个很简单的XML结构,一个图书目录. XML的代码如下: <catalog> <book> <title>XML for Beginners</title> <author>Fred Fnord</author> <description>A book on XML for programming neophytes.</description> </book> <book> <title>Pair-O-Dice Lost</title> <author>U. Wajer</author> <description>Techniques for throwing the game.</description> </book> <book> <title>The Fields of Oberon</title> <author>Alan Landis</author> <description>The wee folk are back, and they aren't happy.</description> </book> <book> <title>Distributed Computing on a Budget</title> <author>Fred Fnord</author> <description>Using XML and related techniques for managing distributed applications.</description> </book> </catalog>