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!
Alexey Smirnov - 09 Aug 2007 16:05 GMT
> I have a string that looks like this:
>
[quoted text clipped - 23 lines]
>
> Thanks for any help or information!
Request\sID:\s(\w+)
or
Request\sID:\s(\d+)
Jesse Houwing - 09 Aug 2007 17:49 GMT
Hello jobs,
> I have a string that looks like this:
>
[quoted text clipped - 21 lines]
> 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