Click here to Skip to main content
13,049,172 members (66,753 online)
Rate this:
Please Sign up or sign in to vote.
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 10-Jan-13 2:25am
What do you mean with format "G40X56.9808Y98.987" ?
Zoltán Zörgő 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2017
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