Regex with double and char

O

O.B.

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);
 
O

O.B.

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);

Odd. The double part of the regular expression is behaving in a non-
greedy mode. Is this a known bug? Changing it to the following gives
a match but opens the code up to false matches (i.e. "345.3.43").

private const string DOUBLE_REGEX = @"[0-9\.]+";
 
J

Jon Skeet [C# MVP]

O.B. said:
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);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top