.NET Framework Bookmark and Share   
 index > Regular Expressions > Validate float values with whitespace and punctation
 

Validate float values with whitespace and punctation

Hi

I want to use regular expressions to validate my web application input. I'm new to regular expressions so excuse me for asking stupidly: I want to validate/match all float/double values like this:

3 500,00
500,50
5,55
3 400 500,00

As you can see I want to handle any number with whitespaces and two decimals. I guess someone takes this easily?

TIA

RightCoder
It's not clear what you want to do with the spaces. Here is an example that mixes Regex with Double.TryParse...

            string[] tests = {
                "3 500,00",
                "500,50",
                "5,55",
                "3 400 500,00",
                              };
            string pattern = @"(?<tryFloat>[\d,.]+)(\s*(?<tryFloat>[\d,.]+))*";
            Thread.CurrentThread.CurrentCulture = new CultureInfo("Fr-fr", true);
            foreach (string test in tests)
            {
                foreach (Match mx in Regex.Matches(test, pattern))
                {
                    foreach (Capture cx in mx.Groups["tryFloat"].Captures)
                    {
                        double dbl = 0.0;
                        if(Double.TryParse(cx.Value,out dbl))
                        {
                            Console.WriteLine("{0} is a valid floating point number",cx.Value);
                        }
                        else
                        {
                            Console.WriteLine("{0} is NOT a valid floating point number",cx.Value);
                        }
                    }
                }
            }


Les Potter, Xalnix Corporation, Yet Another C# Blog
  • Marked As Answer byRightCoder Friday, September 11, 2009 11:56 AM
  •  
xalnix
It's not clear what you want to do with the spaces. Here is an example that mixes Regex with Double.TryParse...

            string[] tests = {
                "3 500,00",
                "500,50",
                "5,55",
                "3 400 500,00",
                              };
            string pattern = @"(?<tryFloat>[\d,.]+)(\s*(?<tryFloat>[\d,.]+))*";
            Thread.CurrentThread.CurrentCulture = new CultureInfo("Fr-fr", true);
            foreach (string test in tests)
            {
                foreach (Match mx in Regex.Matches(test, pattern))
                {
                    foreach (Capture cx in mx.Groups["tryFloat"].Captures)
                    {
                        double dbl = 0.0;
                        if(Double.TryParse(cx.Value,out dbl))
                        {
                            Console.WriteLine("{0} is a valid floating point number",cx.Value);
                        }
                        else
                        {
                            Console.WriteLine("{0} is NOT a valid floating point number",cx.Value);
                        }
                    }
                }
            }


Les Potter, Xalnix Corporation, Yet Another C# Blog
  • Marked As Answer byRightCoder Friday, September 11, 2009 11:56 AM
  •  
xalnix
Hi Xalnix

Thanks for your answer and your code is running and works. All the test is Ok.

Great work!

RightCoder

You can use google to search for other answers

Custom Search

More Threads

• Email regex with VB
• Read from HTML Page then format into Listbox
• Tough reg-ex question: any two (3, n) words groups from text
• RegEx Substitution patterns for Xml Encoding & Decoding
• regex - Pattern Help For Beginner
• removing RTF tags
• Help correcting regular expression
• Regex help required.
• Regularexpressions: remove html code
• Another Replace Question....