Results 1 to 2 of 2

Thread: [RESOLVED] RegEx Help

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [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:
    1. Dim pStr: pStr="UserID:1|UserName:allanc|UserTitle:Mr|UserFirstName:Allan|"
    2. Dim sFind: sFind="UserID"
    3. Dim matches,match
    4.  
    5. Set matches = RegExpr_ReturnMatch(pStr,sFind & ":([^|]+)",True)
    6. If matches.Count > 0 Then
    7.     For Each match In matches
    8.         Response.Write match.Value & "<br>"
    9.     Next
    10. Else
    11.     Response.Write "Not found in the string<br>"
    12. End If
    13.  
    14. Function RegExpr_ReturnMatch(sOriginalString, sPattern, bIgnoreCase)
    15.     Dim objRegExp: Set objRegExp = New RegExp
    16.     With objRegExp
    17.         .Pattern = sPattern
    18.         .IgnoreCase = bIgnoreCase
    19.         .Global = True
    20.     End With
    21.     Set RegExpr_ReturnMatch=objRegExp.Execute(sOriginalString)
    22.     Set objRegExp = Nothing
    23. 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.

  2. #2

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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:
    1. Dim pStr: pStr="UserID:1|UserName:allanc|UserTitle:Mr|UserFirstName:Allan|"
    2. Dim sFind: sFind="UserID"
    3. Dim matches,match,i
    4.  
    5. Set matches = RegExpr_ReturnMatch(pStr,sFind & ":([^|]+)",True)
    6. If matches.Count > 0 Then
    7.     For Each match In matches
    8.         Response.Write match.Value & "<br>"
    9. [HL="#FFFF80"]      If match.SubMatches.Count > 0 Then
    10.             For i=0 To match.SubMatches.Count-1
    11.                 Response.Write match.SubMatches(i) & "<br>"
    12.             Next ' submatch
    13.         End If[/HL]
    14.     Next ' match
    15. Else
    16.     Response.Write "Not found in the string<br>"
    17. End If
    18.  
    19. Function RegExpr_ReturnMatch(sOriginalString, sPattern, bIgnoreCase)
    20.     Dim objRegExp: Set objRegExp = New RegExp
    21.     With objRegExp
    22.         .Pattern = sPattern
    23.         .IgnoreCase = bIgnoreCase
    24.         .Global = True
    25.     End With
    26.     Set RegExpr_ReturnMatch=objRegExp.Execute(sOriginalString)
    27.     Set objRegExp = Nothing
    28. 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
  •  



Click Here to Expand Forum to Full Width