Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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

I am guessing that you want variable length numbers prefixed with the code. Try this:
C#
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"]);
    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900