Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

Regex match excluding search string in result:

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jobs - 09 Aug 2007 16:01 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.