I ran the following console app:
static void Main(string[] args)
{
RegexOptions options = RegexOptions.Ig norePatternWhit espace | RegexOptions.Si ngleline;
string pattern =
@"(?<sentence>. *? ) # lazily match sentence so that '.' does not capture eos
(?<eos(?<! /) # negative lookbehind for /
/)?$ # match single '/' at end of string or just end of string";
// Expected: implicit, outer group match, "My Taylor is " and "/" (3)
Match("My Taylor is /", pattern, options, 3);
// Expected: implicit, outer group match and "My Taylor is //" (2)
Match("My Taylor is //", pattern, options, 2);
// Expected: implicit, outer group match and "My Taylor is / " (2)
Match("My Taylor is / ", pattern, options, 2);
Console.ReadLin e();
}
private static void Match(string input, string pattern, RegexOptions options, int expectedSuccess Count)
{
Match match = Regex.Match(inp ut, pattern, options);
Console.WriteLi ne("Input={0}: {1}", input, (match.Success) ? "Success" : "Failed");
Console.WriteLi ne("Expected # of successful groups: " + expectedSuccess Count);
Console.WriteLi ne();
if (!match.Success )
return;
Console.WriteLi ne("Match: {0}", match);
Console.WriteLi ne();
Console.WriteLi ne("Captures: ");
for (int c = 0; c < match.Captures. Count; c++)
Console.WriteLi ne("{0}: {1}", c, match.Captures[c]);
Console.WriteLi ne();
Console.WriteLi ne("Groups: ");
int actualSuccessCo unt = 0;
for (int g = 0; g < match.Groups.Co unt; g++)
{
if (match.Groups[g].Success)
actualSuccessCo unt++;
Console.WriteLi ne("{0} ({1}): {2}", g, (match.Groups[g].Success) ? "Success" : "Failed", match.Groups[g]);
}
Console.WriteLi ne();
Console.WriteLi ne("Success: {0}", (expectedSucces sCount == actualSuccessCo unt) ? "Yes" : "No");
Console.WriteLi ne(new string('-', 10));
Console.WriteLi ne();
}
which produced the following output:
Input=My Taylor is /: Success
Expected # of successful groups: 3
Match: My Taylor is /
Captures:
0: My Taylor is /
Groups:
0 (Success): My Taylor is /
1 (Success): My Taylor is
2 (Success): /
Success: Yes
----------
Input=My Taylor is //: Success
Expected # of successful groups: 2
Match: My Taylor is //
Captures:
0: My Taylor is //
Groups:
0 (Success): My Taylor is //
1 (Success): My Taylor is //
2 (Failed):
Success: Yes
----------
Input=My Taylor is / : Success
Expected # of successful groups: 2
Match: My Taylor is /
Captures:
0: My Taylor is /
Groups:
0 (Success): My Taylor is /
1 (Success): My Taylor is /
2 (Failed):
Success: Yes
----------
I think this meets the requirements set by Jules' in the OP.
Jules, can you please comment to the effectiveness of the expression according to your requirements?
--
Dave Sexton
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message news:%2******** **********@TK2M SFTNGP06.phx.gb l...
Hi,
I'm not sure where the problem is.
I tested the expression and it worked according to the op's requirements. Is it returning extra information that the op doesn't
require? Does that matter? Is there a better way?
--
Dave Sexton
"Barry Kelly" <ba***********@ gmail.comwrote in message news:l5******** *************** *********@4ax.c om...
>Jules wrote:
>>Hi John - I'm not sure what you mean by matching the empty string after
'/'
A regular expression which is composed entirely of elements which can
match zero-width strings (foo*, foo?, foo{0,<whatever >}) itself matches
each zero-width string (i.e. the gap between each character). The
difference with this regular expression is that it contains an assertion
at the end - the '$' - that's what stops it finding every zero-length
string between each character in the source. Instead, it finds only the
last one (as well as the actual match).
-- Barry
--
http://barrkel.blogspot.com/