Click here to Skip to main content
Rate this: bad
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" to validate it using regex in c#..any one could help me.
Posted 9-Jan-13 20:18pm
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
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"]);

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

  Print Answers RSS
0 OriginalGriff 259
1 Sergey Alexandrovich Kryukov 182
2 Hard_Rockz 153
3 Richard MacCutchan 125
4 Maciej Los 104
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270

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