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 / .NET Framework / New Users / April 2005

Tip: Looking for answers? Try searching our database.

Regular expression captures

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Erik Tamminga - 17 Apr 2005 22:08 GMT
Hi,

From the following string I would like to capture text.

String: This action has been executed via Console

First of all I like to know if this is the text I need: it must contain
"This action has been executed via".
Secondly I would like to know "Console"

How can Regular expressions help me in this. I've tried various things with
the RegEx class but I'm having troubles retrieving "Console". The
match.Captures collection always seems to contain the string searched, not
only the captured part.

RegEx r = RegEx( "This action has been executed", RegExOptions.IgnoreCase)
if (r.Matches( myString).Count > 0)
{
   Here matches are found and r.Matches[0].Captures.Count > 0.
   Why is ...Captures.Count > 0, I did ask for a capture?
}

RegEx q = RegEx( "(C\w+)", RegExOptions.IgnoreCase)
if (q.Matches( myString).Count > 0)
{
   Here matches are found and r.Matches[0].Captures.Count > 0.
   This is where I expected the capture!!
}

regards,

Erik
Jon Shemitz - 17 Apr 2005 22:42 GMT
> From the following string I would like to capture text.
>
[quoted text clipped - 8 lines]
> match.Captures collection always seems to contain the string searched, not
> only the captured part.

The captures will always include the substring that matches the whole
regex in the Match object and in Groups[0]. Explicit captures start at
Groups[1]. This is by design, and broadly matches the behavior of Perl
regexes.

Try using

 Regex("This action has been executed via (.*)",
   RegExOptions.IgnoreCase)

and looking at Groups[1].

> if (r.Matches( myString).Count > 0)

Don't write code like this. Readings Matches.Count forces the regex to
find every match. Use

 Match M = R.Match(Text);
 if (M.Success)
    string Capture = M.Groups[1].Value;

Signature

www.midnightbeach.com


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.