Re: Regular expression captures



Erik Tamminga wrote:

> 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.

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;

--

www.midnightbeach.com
.



Relevant Pages

  • Regular expression captures
    ... String: This action has been executed via Console ... RegEx r = RegEx ... This is where I expected the capture!! ...
    (microsoft.public.dotnet.framework)
  • Re: yet another regex
    ... I thought it takes in the string as one entity and just captures ... the first digit it can. ... The g on the end of the regex causes it to apply the pattern multiple ...
    (perl.beginners)
  • Re: wprintf() could not display unicode chars which is > 255 to console?
    ... > console window with formatting strings. ... Unicode and the Windows console is a tricky business. ... string msg, action; ... In Japanese you should have no spaces, and the translator can remove them. ...
    (microsoft.public.vc.mfc)
  • Small regular expression parser
    ... the goal was to develop a very simple regular expression parser. ... sets are selected using the % character instead of \. ... into the string of the start of the match and the length of the match. ... Last there are a couple macros to help with captures. ...
    (comp.lang.lisp)
  • Re: Command Line Interface
    ... This is somewhat I am aiming for, to be honest my idea for this is Java ... Netbean's 'console window' which it uses for user input and output. ... [user enters string and conv to double] ... CString GetString() ...
    (microsoft.public.vc.mfc)