|
-
Jan 30th, 2007, 12:19 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] RegEx Help
Hi,
I'm trying to use asp (vbscript) regular expressions to extract information from a delimited string, with the following code when I loop through the matches it returns the entire match, I know that the value that I'm after is in $1, but I can't work out how to get hold of it. Does anyone have any ideas? Or a better approach?
e.g. Match="UserID:1"
I know that $1=1 <-- this is the value I want
VB Code:
Dim pStr: pStr="UserID:1|UserName:allanc|UserTitle:Mr|UserFirstName:Allan|"
Dim sFind: sFind="UserID"
Dim matches,match
Set matches = RegExpr_ReturnMatch(pStr,sFind & ":([^|]+)",True)
If matches.Count > 0 Then
For Each match In matches
Response.Write match.Value & "<br>"
Next
Else
Response.Write "Not found in the string<br>"
End If
Function RegExpr_ReturnMatch(sOriginalString, sPattern, bIgnoreCase)
Dim objRegExp: Set objRegExp = New RegExp
With objRegExp
.Pattern = sPattern
.IgnoreCase = bIgnoreCase
.Global = True
End With
Set RegExpr_ReturnMatch=objRegExp.Execute(sOriginalString)
Set objRegExp = Nothing
End Function
The following pattern "(?<=UserID:)([^|]+)" works here, but when I use it I get the following error, guess it's a .net / asp classic difference.
Syntax error in regular expression
Any help will be greatly appreciated
Cheers Al
Last edited by aconybeare; Jan 31st, 2007 at 09:36 AM.
-
Jan 31st, 2007, 09:34 AM
#2
Thread Starter
Fanatic Member
Re: RegEx Help
Hi, I believe I've found the answer. The code is as above, but have added in the highlighted loop through the sub matches collection.
VB Code:
Dim pStr: pStr="UserID:1|UserName:allanc|UserTitle:Mr|UserFirstName:Allan|"
Dim sFind: sFind="UserID"
Dim matches,match,i
Set matches = RegExpr_ReturnMatch(pStr,sFind & ":([^|]+)",True)
If matches.Count > 0 Then
For Each match In matches
Response.Write match.Value & "<br>"
[HL="#FFFF80"] If match.SubMatches.Count > 0 Then
For i=0 To match.SubMatches.Count-1
Response.Write match.SubMatches(i) & "<br>"
Next ' submatch
End If[/HL]
Next ' match
Else
Response.Write "Not found in the string<br>"
End If
Function RegExpr_ReturnMatch(sOriginalString, sPattern, bIgnoreCase)
Dim objRegExp: Set objRegExp = New RegExp
With objRegExp
.Pattern = sPattern
.IgnoreCase = bIgnoreCase
.Global = True
End With
Set RegExpr_ReturnMatch=objRegExp.Execute(sOriginalString)
Set objRegExp = Nothing
End Function
May help someone else sometime.
Cheers Al
Last edited by aconybeare; Jan 31st, 2007 at 09:37 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|