473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Regex with double and char

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

Oct 15 '07 #1
2 3975
On Oct 15, 12:25 pm, "O.B." <funkj...@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);
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\.]+";
Oct 15 '07 #2
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
Oct 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
4
by: JS | last post by:
I am writing a C# app that needs to parse a sentence entered by the user for a simple boolean search. I need to capture all of the AND words that are not inside of double quotes. However, I am...
5
by: FBergemann | last post by:
I use SunOS 5.8, gcc 3.3.2, boost 1.33.1. I have build the entire boost package and try to compile a simple example: #include <iostream> #include <string> #include <boost/regex.hpp //...
10
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: bob | last post by:
Hi I have a long regex string that needs to match the quote char. It is matching a CSV text file where all data fields are encased in quotes. Some fields are empty ie double quotes. I want to make...
3
by: =?Utf-8?B?TWFya19C?= | last post by:
The following is working for me but I want to include numbers in scientific notation. public double Evaluate( string expr ) { const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)" Regex reMulDiv = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.