Regex match excluding search string in result:

Discussion in 'ASP .Net' started by jobs, Aug 9, 2007.

  1. jobs

    jobs Guest

    I have a string that looks like this:

    'ACCEPT - The transaction succeeded. Merchant Reference Code:OrderId
    Request ID: 1865550982350176174087 Authorized Amount: 10.00
    Authorization Code: 123456 Authorization Time: 2007-08-09T14:18:18Z

    I need to pull out that string after "Request ID: "

    which in this case should be :

    1865550982350176174087

    This returns the full string with the search string:

    Dim regexp As Regex = New Regex("Request ID: (\w+)",
    RegexOptions.IgnoreCase)
    Dim TransId As String = regexp.Match(CCOut).ToString
    StatusLabel.Text = TransId.ToString

    and this returns nothing:

    Dim pattern As String = "Request ID: (\w+)" & Regex.Escape("Request
    ID: ")
    Dim match As Match = Regex.Match(CCOut, pattern)
    StatusLabel.Text = match.Value.ToString

    Thanks for any help or information!
    jobs, Aug 9, 2007
    #1
    1. Advertising

  2. On Aug 9, 5:01 pm, jobs <> wrote:
    > I have a string that looks like this:
    >
    > 'ACCEPT - The transaction succeeded. Merchant Reference Code:OrderId
    > Request ID: 1865550982350176174087 Authorized Amount: 10.00
    > Authorization Code: 123456 Authorization Time: 2007-08-09T14:18:18Z
    >
    > I need to pull out that string after "Request ID: "
    >
    > which in this case should be :
    >
    > 1865550982350176174087
    >
    > This returns the full string with the search string:
    >
    > Dim regexp As Regex = New Regex("Request ID: (\w+)",
    > RegexOptions.IgnoreCase)
    > Dim TransId As String = regexp.Match(CCOut).ToString
    > StatusLabel.Text = TransId.ToString
    >
    > and this returns nothing:
    >
    > Dim pattern As String = "Request ID: (\w+)" & Regex.Escape("Request
    > ID: ")
    > Dim match As Match = Regex.Match(CCOut, pattern)
    > StatusLabel.Text = match.Value.ToString
    >
    > Thanks for any help or information!


    Request\sID:\s(\w+)

    or

    Request\sID:\s(\d+)
    Alexey Smirnov, Aug 9, 2007
    #2
    1. Advertising

  3. Hello jobs,

    > I have a string that looks like this:
    >
    > 'ACCEPT - The transaction succeeded. Merchant Reference Code:OrderId
    > Request ID: 1865550982350176174087 Authorized Amount: 10.00
    > Authorization Code: 123456 Authorization Time: 2007-08-09T14:18:18Z
    >
    > I need to pull out that string after "Request ID: "
    >
    > which in this case should be :
    >
    > 1865550982350176174087
    >
    > This returns the full string with the search string:
    >
    > Dim regexp As Regex = New Regex("Request ID: (\w+)",
    > RegexOptions.IgnoreCase)
    > Dim TransId As String = regexp.Match(CCOut).ToString
    > StatusLabel.Text = TransId.ToString
    > and this returns nothing:
    >
    > Dim pattern As String = "Request ID: (\w+)" & Regex.Escape("Request
    > ID: ")
    > Dim match As Match = Regex.Match(CCOut, pattern)
    > StatusLabel.Text = match.Value.ToString
    > Thanks for any help or information!
    >


    You have two options here
    1) use grouping to extract the number with the regex you're currently using
    2) use a look behind to exclude the "request ID:" from the match completely.

    Solution 1:
    ========================================================
    Instead of using match.Value to get the contents of the whole match (Value
    is already of type string, so there's no need to call ToString here), you
    can use a capturing group to get the values:

    Dim regex As Regex = new Regex("Request ID: (\w+)", RegexOptions.IgnoreCase)
    Dim match As Match = regex.Match(CCOut, pattern)
    if match.Success
    StatusLabel.Text = match.Groups[1].Value // not sure if you need to use
    Groups[] or Groups() in VB.Net

    Or if you name thr group, which I always find easier to read you can use:

    Dim regex As Regex = new Regex("Request ID: (?<requestid>\w+)", RegexOptions.IgnoreCase)
    Dim match As Match = regex.Match(CCOut, pattern)
    if match.Success
    StatusLabel.Text = match.Groups["requestid"].Value // not sure if you need
    to use Groups[] or Groups() in VB.Net

    Solution 2:
    ========================================================
    by using (?<=...) you can ensure there is a text right in front of your match.
    But it does not become part of the match itself.

    Dim regex As Regex = new Regex("(?<=Request ID: )\w+", RegexOptions.IgnoreCase)
    Dim match As Match = regex.Match(CCOut, pattern)
    if match.Success
    StatusLabel.Text = match.Value // not sure if you need to use Groups[] or
    Groups() in VB.Net

    You might want to try which one is faster. Or decide which one you find easier
    to read (which in turn means easier to maintain).

    Jesse
    Jesse Houwing, Aug 9, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    627
  2. Franz Steinhaeusler

    excluding search string in regular expressions

    Franz Steinhaeusler, Oct 21, 2004, in forum: Python
    Replies:
    9
    Views:
    1,133
    Franz Steinhaeusler
    Oct 22, 2004
  3. kimimaro
    Replies:
    4
    Views:
    288
    Barry Schwarz
    Nov 1, 2004
  4. Michael Tan
    Replies:
    32
    Views:
    927
    Ara.T.Howard
    Jul 21, 2005
  5. Replies:
    14
    Views:
    90
    Thomas 'PointedEars' Lahn
    May 21, 2014
Loading...

Share This Page