PDA

View Full Version : Regular Expression Issue


aspN00b
07-05-2009, 02:43 AM
Hi there,

This is my first post. I normally program in Python and I've been asked to modify an ASP script. It's not going well :(

I would like to make an analogous statement to this python statement in ASP:

tags = re.findall('(<div>)(.{0,255})(</div>)', mainurl)
for tag in tags:
print tag[1]




I tried this to no avail :( :

Dim response(20)

Dim matchposition(2)

Dim regEx

Dim Matches

' Create a new Regex object and define the regular expression.
Set regEx = new RegExp
regEx.Pattern = "((<div>)(.{0,100})(</div>))"
' Use the Matches method to find all matches in the input string.
'MatchCollection mc = re.Matches(description)
Matches = regEx.Execute(description)
Response.Write Matches
' Loop through the match collection to retrieve all
' matches and positions.
Dim i
For i = 0 To mc.Count - 1
' Add the match string to the string array.
results(i) = mc(i).Value
Response.Write mc(i).Value
' Record the character position where the match was found.
matchposition(i) = mc(i).Index
Next i

shyam
07-05-2009, 07:18 AM
you assign the output of the execute to Matches and never use it again :|

aspN00b
07-05-2009, 06:51 PM
I actually get an error right off bat. ASP doesn't even like this:

Dim response(20)


It says "type mismatch"

After that, the next error I get is on this line:


Matches = regEx.Execute("slkkdjfsldjfs<div>skdjfsd</div>skljfsdlfjsfdk")
Wrong number of arguments or invalid property assignment

Old Pedant
07-05-2009, 10:17 PM
Response is a keyword (as in Response.Write, Response.Redirect, etc.) so it can't be used as a variable name. Choose another name. Even responses would be okay (remember, VBScript is *NOT* case sensitive)..

Old Pedant
07-05-2009, 10:55 PM
Here...a sample nearly directly out of the VBScript documentation:

<%
html = "this is <div>just a sample</div> with a couple of <DIV>divs in it </DIV>, see?"


' Create a new Regex object and define the regular expression.
Set regEx = new RegExp
regEx.Pattern = "((<div>)(.*?)(</div>))"
regEx.IgnoreCase = True
regEx.Global = True

' Generalized code to find all matches and submatches:
Set Matches = regEx.Execute(html)
Response.Write "<ul>"
For i = 0 To Matches.Count - 1
Set match = Matches(i)
Response.Write "<li>" & Server.HTMLEncode(match.Value) & "::firstIndex @ " & match.FirstIndex & "</li><ul>" & vbNewLine
Set subm = match.Submatches
For s = 0 To subm.Count-1
Response.Write "<li>" & Server.HTMLEncode(subm(s)) & "</li>"
Next
Response.Write "</ul>" & vbNewLine
Next
Response.Write "</ul>"

%>

The HTMLEncode is used, of course, so that we can see the <div> tags that are found.

aspN00b
07-06-2009, 05:08 AM
Old Pedant I could kiss you! This is a pretty awesome community and I look forward to giving back!