Class code for ASP parsing HTML using positive expressions

Source: Internet
Author: User
Tags count end expression html tags include new features regular expression trim
Many new features have been added to the VBScript5, the most inspiring of which is the emergence of generic and regular expressions. Here is a class that I write to parse the HTML code. I am learning PHP, grammar has not used to place, please include more.
<%
Class Htmlparse
' Set the Initialize event.
Private Sub Class_Initialize
Myglobal = True
Myignorecase = True
End Sub

Property Let Global (g)
Dim regEx ' establishes a variable.
Set regEx = New RegExp ' establishes a regular expression.
Regex.pattern = "true| False|1|0 "' Set mode.
Regex.ignorecase = True ' Sets whether case sensitive.
If Regex.test (CStr (g)) Then
Myglobal = g
Else
Call Halt ("invalid global parameter Configuration")
End If
End Property

Property Get Global ()
Global = Myglobal
End Property

Property Let IgnoreCase (c)
Dim regEx
Set regEx = New RegExp
Regex.pattern = "true| False|1|0 "
Regex.ignorecase = True
If Regex.test (CSTR (c)) Then
Myignorecase = C
Else
Call Halt ("invalid ignorecase parameter Configuration")
End If
End Property

Property Get IgnoreCase ()
IgnoreCase = Myignorecase
End Property

' Parse functions for all HTML tags
Public Function Parse (input)
Parse = "<table border=1 width=50% align=center>" & vbCrLf
Dim regEx, Regval, match, I

Set regEx = New RegExp
Regex.pattern = "< ([a-z]\w*) (?:. *?) > (. *) <\/\1> "
Regex.global = Myglobal
Regex.ignorecase = Myignorecase

Set regval = Regex.execute (Trim (input))
If regval.count > 0 Then ' If a matching element is found
Parse = Parse & "<caption> discovery" & regval.count & "HTML tag </caption>" & VbCrLf
Parse = Parse & "<tr align=center><th> numbering </th><th> matching tags <th> matching display </th></tr > "& vbCrLf
For I=0 to Regval.count-1
Set match = Regval (i)
Parse = Parse & "<tr align=center>" & vbCrLf
Parse = Parse & "<td>" & i+1 & "</td><td>" & match. Submatches (0) & "</td><td>" & Match
& "</td>" & VbCrLf
Parse = Parse & "</tr>" & VbCrLf
Next
Else Parse = Parse & "<caption> no HTML tags found </caption>" & VbCrLf
End If
Parse = Parse & "</table>" & VbCrLf
End Function

' Parse the function of the specified HTML tag
Public Function Parsetag (Input,tag)
Parsetag = "<table border=1 width=50% align=center>" & vbCrLf
Dim regEx, Regval, match, I

Set regEx = New RegExp
Regex.pattern = "< (" & Tag & ") (?:. *?) > (. *?) <\/\1> "
Regex.global = Myglobal
Regex.ignorecase = Myignorecase

Set regval = Regex.execute (Trim (input))
If regval.count > 0 Then ' If a matching element is found
Parsetag = Parsetag & "<caption> discovery" & Regval.count & "A" & UCase (TAG) & "Mark </caption>" &am P
VbCrLf
Parsetag = Parsetag & "<tr align=center><th> number </th><th> discovery location <th> include content </th> </tr> "&
VbCrLf
For I=0 to Regval.count-1
Set match = Regval (i)
Parsetag = Parsetag & "<tr align=center>" & vbCrLf
Parsetag = Parsetag & "<td>" & i+1 & "</td><td>" & match. FirstIndex & "</td><td>" &
Match. Submatches (1) & "</td>" & VbCrLf
Parsetag = Parsetag & "</tr>" & VbCrLf
Next
Else Parsetag = parsetag & "<caption> not Found" & UCase (TAG) & "Mark </caption>" & VbCrLf
End If
Parsetag = Parsetag & "</table>" & VbCrLf
End Function

' Print error
Private Sub Halt (ERRSTR)
Response.Write ("<font color=red size=3>" & Errstr & "</font>" & VbCrLf)
Call Class_Terminate
End Sub

Private Sub class_terminate ' sets Terminate event.
End Sub

' Define two internal variables
Private Myglobal
Private Myignorecase

End Class
%>

<body>
<div align=center><%
' Example 1
Dim input, result
Input = "<i> This is </i> a <font color=green>vbscript</font> <b> regular <i> expression </i> example </b>. "

Set hp = New Htmlparse
Hp. Global = 1
Hp. IgnoreCase = False
result = HP. Parse (Input)
Response.Write (Result)
%>
<br>
<div align=center><%
' Example 2
' HP. Global = 1
' HP. IgnoreCase = False
RESULT2 = hp. Parsetag (Input, "I")
Response.Write (RESULT2)
Set hp = Nothing
%>
</body>

It should be noted that VBScript's regular expression is the same as JScript parsing, except that the syntax is different. For the latest VBScript documents, you can go to Microsoft China's Scripting Technology homepage to download, url:http://www.microsoft.com/china/scripting



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.