473,760 Members | 8,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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

Similar topics

7
5728
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 is to improve, but not to prove.
9
4588
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
728
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 ignore pairs of brackets like: "one" <tab> "two three" ( four "five six" ) Want fields like:
4
3206
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 having a heck of a time figuring out a regex for it. Can anyone assist with a regex to find all the AND's not in double quotes? An example sentence might be: red and blue and "crazy elephant" and "orange and red" and stuff.
5
7191
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 // Boost.Regex lib using namespace std;
10
3892
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 is using Python's standard re (regular expressions) module to parse the input file. As Python's routines to read from the file and regular expressions are most likely implemented via native libraries I would expect that the C code, which reads...
15
50258
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
3696
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 sure all fields are correctly formed. Is there any easy way to embed the '"" in the string or do I have to break it up into something like char quote = '"'; string myregex = "\\A" +quote + ",(*)" + quote + "," + quote +",(*)" etc etc.
3
2196
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 Regex(Num + @"\s*()\s*" + Num); other stuff:
0
9521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9945
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7324
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.