Click here to Skip to main content
Sign Up to vote bad
good
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 - 20:18

Comments
Andy411 - 10 Jan '13 - 2:25
What do you mean with format "G40X56.9808Y98.987" ?
Zoltán Zörgő - 10 Jan '13 - 2:39
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

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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 498
1 Arun Vasu 275
2 Maciej Los 273
3 Mahesh Bailwal 264
4 Aarti Meswania 175
0 Sergey Alexandrovich Kryukov 9,660
1 OriginalGriff 7,329
2 CPallini 3,968
3 Rohan Leuva 3,339
4 Maciej Los 2,851


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid