O.B. <fu******@bells outh.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_REGE X = @"[N|S|E|W]";
string sourceString = "550402N0420502 .50S";
string matchPattern = "(" + DOUBLE_REGEX + ")+" +
"(" + HEMISPHERE_REGE X + ")+" +
"(" + DOUBLE_REGEX + ")+" +
"(" + HEMISPHERE_REGE X + ")+";
Regex evaluator = new Regex(matchPatt ern);
MatchCollection matches = evaluator.Match es(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.Reg ularExpressions ;
public class Test
{
private const string DOUBLE_REGEX = @"[-|+]?[0-9]*[.]?[0-9]*";
private const string HEMISPHERE_REGE X = @"[N|S|E|W]";
static void Main()
{
string sourceString = "550402N0420502 .50S";
string matchPattern = "(" + DOUBLE_REGEX + ")" +
"(" + HEMISPHERE_REGE X + ")" +
"(" + DOUBLE_REGEX + ")" +
"(" + HEMISPHERE_REGE X + ")";
Regex evaluator = new Regex(matchPatt ern);
MatchCollection matches = evaluator.Match es(sourceString );
foreach (Group group in matches[0].Groups)
{
Console.WriteLi ne(group.Value) ;
}
}
}
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too