locked
Matching Numeric String with RegEx RRS feed

  • Question

  • User1122355199 posted

    Hello Everyone and thanks for your help in advance.  I am working on a application that needs to parse URLs.  The general format of the URL is:

    profile.asp?LicId=96651

    I have tried the following:

    profile.asp\?LicId\=[0-9]

    However, this only retrieves the first number of the LicId.  I have tried various wildcard characters without any success.  Any help on this issue would be greatly appreciated.

    Saturday, October 4, 2008 9:37 PM

All replies

  • User-132834496 posted

    For numbers you can use \d {n} where n is the number of digits. If your number of digits vary, say from 5 digits to 8 digits, then you can use \d{5,8}. Basing on this example you can write for how many digits you want

    Saturday, October 4, 2008 11:49 PM
  • User1122355199 posted

    Thanks for the response.  I gave this a try, but still can't seem to get thigns quite right.  Here is some more information.  First, here are a few of the strings I am trying to work with:

    profile.asp?LicId=87803&ProfNBR=1501

    profile.asp?LicId=5934&ProfNBR=1901

    As you can see, the numeric string after the LicId= is variable in length.  I actually don't need to match anything after the numeric portion, so I can let the & termminate the match.  The code I have tried is:

    Dim MatchPattern As String = "profile.asp\?LicId\=[\d{2-6}]"

    Dim mcLinks As MatchCollection
    Dim mcLink As Match

    mcLinks = Regex.Matches(html, MatchPattern, RegexOptions.IgnoreCase)
    For Each mcLink In mcLinks
        LinkData = LinkData & mcLink.ToString & vbCrLf
    Next

    However, the output returned is:

    profile.asp?LicId=8
    profile.asp?LicId=5

    Not really sure why this isn't working.  Once again, thanks for the help. 

     

    Sunday, October 5, 2008 7:26 AM
  • User-132834496 posted

    There is no problem in regular expression. Even the expression you have written earlier also should work. It seems there is some problem in your code. As I am not familiar with VB.NET, I am sorry I cannot tell where the mistake lies.

    Anyway the code block you have written here also looks right upto my understanding. Please check  any other relevant code blocks  and datatypes.. there might be some simple mistake.

    Sunday, October 5, 2008 8:39 AM
  • User-1410257499 posted

    Have you looked at using the My.Request.QueryString function?

    Dim coll As NameValueCollection = My.Request.QueryString
    
    Dim LicID As String = coll.Item("LicId").Trim ' LicId then equals "87803"
    Dim ProfNBR As String = coll.Item("ProfNBR").Trim 'ProfNBR equals "1501"
    
    This should work well for you.
    Friday, October 10, 2008 1:16 AM
  • User-990694832 posted

    Change the regex to "=\d+"

    This will find all the numeric matches, 2 in the string provided licid and prfnbr

    I used http://www.myregextester.com/index.php to test the expression, the site also gives a code example if needed

    Hope this helps

    DK

     

    Friday, October 10, 2008 9:24 AM