O.B. <fu******@bellsouth.netwrote:
In the following example, the Matches operation never returns 4
matches as I am expecting. What's wrong with my syntax?
private const string DOUBLE_REGEX = @"[-|+]?[0-9]*[.]?[0-9]*";
private const string HEMISPHERE_REGEX = @"[N|S|E|W]";
string sourceString = "550402N0420502.50S";
string matchPattern = "(" + DOUBLE_REGEX + ")+" +
"(" + HEMISPHERE_REGEX + ")+" +
"(" + DOUBLE_REGEX + ")+" +
"(" + HEMISPHERE_REGEX + ")+";
Regex evaluator = new Regex(matchPattern);
MatchCollection matches = evaluator.Matches(sourceString);
You're only trying to find one *match*, but with multiple *groups*
inside it.
Your "+" after each of the double expressions is also making things
harder, as the empty string originally matches your whole double regex
as well.
Here's some sample code which prints out all the groups of a slightly
altered regex. Note that the first group is implicitly "the whole
match".
using System;
using System.Text.RegularExpressions;
public class Test
{
private const string DOUBLE_REGEX = @"[-|+]?[0-9]*[.]?[0-9]*";
private const string HEMISPHERE_REGEX = @"[N|S|E|W]";
static void Main()
{
string sourceString = "550402N0420502.50S";
string matchPattern = "(" + DOUBLE_REGEX + ")" +
"(" + HEMISPHERE_REGEX + ")" +
"(" + DOUBLE_REGEX + ")" +
"(" + HEMISPHERE_REGEX + ")";
Regex evaluator = new Regex(matchPattern);
MatchCollection matches = evaluator.Matches(sourceString);
foreach (Group group in matches[0].Groups)
{
Console.WriteLine(group.Value);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too