Re: Regular expression captures
- From: Jon Shemitz <jon@xxxxxxxxxxxxxxxxx>
- Date: Sun, 17 Apr 2005 14:42:15 -0700
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
.
- References:
- Regular expression captures
- From: Erik Tamminga
- Regular expression captures
- Prev by Date: Regular expression captures
- Next by Date: Re: Why does aspx pages crash my machine?
- Previous by thread: Regular expression captures
- Next by thread: RE: .net framework V2.0.40903
- Index(es):
Relevant Pages
|