Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Regex with double and char

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
O.B. - 15 Oct 2007 18:25 GMT
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.B. - 15 Oct 2007 19:14 GMT
> In the following example, the Matches operation never returns 4
> matches as I am expecting.  What's wrong with my syntax?
[quoted text clipped - 11 lines]
> 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\.]+";
Jon Skeet [C# MVP] - 15 Oct 2007 19:22 GMT
> In the following example, the Matches operation never returns 4
> matches as I am expecting.  What's wrong with my syntax?
[quoted text clipped - 11 lines]
> 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);
       }
   }
}

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.