PDA

View Full Version : ASP Regular Expression Help


sin010101
05-13-2010, 08:31 PM
I am trying to pull out an ID number from a string, but cannot figure it out. I have this regex:

Dim string
string = "newline <html><body>newline NAME: John Doe<br />SUBSCRIBER ID: 0010101 <span>more text</span>"

Set SubsriberRegEx = New RegExp
With SubsriberRegEx
.Pattern = "SUBSCRIBER\s+ID:\s+(\d+)"
.IgnoreCase = True
.Global = True
End With

What i want to do is just print out $1, the closest thing i have found is:
Response.Write(SubsriberRegEx.Replace(string, "Subscriber ID: $1"))

Which Prints out:
Subscriber ID: 0010101 more text

Notice the "more text". The problem is that the variable "string" is actually a huge file with a lot of weird characters inside of it.

I know i could change this example regex to:
.Pattern = "SUBSCRIBER\s+ID:\s+(\d+).*"

and that would work in this example, but the file I am using has a lot of characters that ".*" do not match, such as special characters, tabs, newlines.

Is there a way to only access $1 without using the Replace method?
maybe something like:
Set SubscriberID = SubsriberRegEx.Execute(string).$1

Any help would be much appreciated.

Old Pedant
05-13-2010, 11:17 PM
str = "newline <html><body>newline NAME: John Doe<br />SUBSCRIBER ID: 0010101 <span>more text</span>"

Set re = New RegExp
re.Pattern = "SUBSCRIBER\s+ID:\s+(\d+)"
re.IgnoreCase = True
Set matches = re.Execute(str)

Response.Write matches(0).submatches(0)


You do not want to use re.Global=true unless you are trying to find multiple occurrences of this expression.

sin010101
05-13-2010, 11:22 PM
Old Pedant,
Thank you so much! This is exactly what i was looking for! You are indeed a master coder :D

sin010101
05-17-2010, 04:25 PM
I am having a new issue now. I am getting an error when there is no match, how do I test for existence on matches(0).submatches(0) ?
I need something like:

If matches(0).submatches(0) <> "" Then

End If

But this does not work, any help would be appreciated.

Old Pedant
05-17-2010, 06:56 PM
Set re = New RegExp
re.Pattern = "SUBSCRIBER\s+ID:\s+(\d+)"
re.IgnoreCase = True
Set matches = re.Execute(str)
If matches.Count > 0 Then
Response.Write matches(0).submatches(0)
Else
Response.Write "No match"
End If

sin010101
05-18-2010, 06:02 PM
Old Pendant,
Please let me know if I am asking too many questions, I will understand. I have just never been able to pick someones brain who is an expert at ASP. What I am trying to do is convert a perl program over to ASP and I am now having an issue trying to replicate the data structure, here is the perl code:

$hashmania{$subscriber}{'TOTALCOUNT'} = $totalcnt;

I have tried using the dictionary object, but it does not support subkeys. the closest thing i have found is this:

Response.Cookies("Mammals")("Dolphin") = "Bottlenosed"

Is there a way to create an object with subkeys without setting a million cookies?

Thank you so much for the help you have provided so far, I have increased your reputation on this site as much as it will let me =D

Old Pedant
05-18-2010, 07:56 PM
Well, VBScript is remarkably poor in collection types. Indeed the Scripting.Dictionary is the only sort of universal collection available. And you *could* have subkeys by having a dictionary of dictionaries.

Set hash = Server.CreateObject("Scripting.Dictionary")
hash.Add "Mammals", Server.CreateObject("Scripting.Dictionary")
hash("Mammals").Add "Dolphins", whatever

And you could of course create your own VBScript Class that would encapsulate that (or even invent a written-in-VBS hash table, which I did many many years ago).

But maybe the right answer, if you are stuck using ASP, would be to use JScript as your scripting language instead of VBScript. JScript is 98% the same as ECMAScript, so the same as JavaScript as used in a browser.

And then you could do
var hash = [];
hash["Mammals"] = [];
hash["Mammals"]["Dolphins"] = whatever;

just exactly the way you'd do it in JS in the browser.