Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# RegEx
I want to validate a equation it should be in a format "G40X56.9808Y98.987"...how to validate it using regex in c#..any one could help me.
Posted 9-Jan-13 21:18pm
Comments
Andy411 at 10-Jan-13 2:25am
   
What do you mean with format "G40X56.9808Y98.987" ?
Zoltán Zörgő at 10-Jan-13 2:39am
   
Please clarify the format G40X56.9808Y98.987? Can it be: "G"; an integer; "X"; a positive float with optional decimal part; "Y";a positive float with optional decimal part?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I am guessing that you want variable length numbers prefixed with the code. Try this:
Regex reg = new Regex(@"G(?<G>\d+(\.\d+){0,1})X(?<X>\d+(\.\d+){0,1})Y(?<Y>\d+(\.\d+){0,1})");
Match m = reg.Match("G40X56.9808Y98.987");
if (m.Success)
    {
    Console.WriteLine("{0}:{1}", "G", m.Groups["G"]);
    Console.WriteLine("{0}:{1}", "X", m.Groups["X"]);
    Console.WriteLine("{0}:{1}", "Y", m.Groups["Y"]);
    }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 300
1 Sergey Alexandrovich Kryukov 287
2 OriginalGriff 195
3 Zoltán Zörgő 149
4 Peter Leow 115
0 OriginalGriff 7,510
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 5,044
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100