Regex Pattern - return only 0-9 and '.' character

I'm trying to return doubles from a string.  The problem is the strings are sometimes full of extra characters.  How can I return only the numbers 0-9 and the decimal character from a string using the Regex class?

so "$10.54" becomes 10.54

and "12/RL" becomes 12.

Thanks.
tradelineAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi tradeline;

This will do it for you.

using System.Text.RegularExpressions;

      string data = "$10.54";
      Match m = Regex.Match(data, @"(\d+\.?\d*)");
      string NumValue = "";
      if( m.Success )
      {
            NumValue = m.Groups[1].Value;
            MessageBox.Show("Number value = " + NumValue.ToString());
      }


Fernando
0
tradelineAuthor Commented:
Fernando,

Doesn't quite work.

for example:

            string dirty = "\"4,200\"";
            string NumValue = String.Empty;
            Match m = Regex.Match(dirty, @"(\d+\.?\d*)");

            if (m.Success)
            {
                NumValue = m.Groups[1].Value;
            }

In this example, NumValue will return "4", when in fact I'd like it to return "4200"

Thanks.
0
Fernando SotoRetiredCommented:
Hi tradeline;

According to your original question these were the patterns you wanted to parse and the Regex I gave you will work on them.

    so "$10.54" becomes 10.54
    and "12/RL" becomes 12.

With the new specification you gave we need to find and replace characters so the code to do that is as follows:

      string dirty = "\"4,200\"";
      string NumValue = String.Empty;
      NumValue = Regex.Replace(dirty, @"(.*?([\d,]+)(\.?)(\d*).*)",
            new MatchEvaluator(FormatNumbes));

      private string FormatNumbes( Match m )
      {
            string part1 = m.Groups[2].Value;
            string part2 = m.Groups[3].Value;
            string part3 = m.Groups[4].Value;
            part1 = part1.Replace(",", "");
            part2 = ".";
            if( part3 == string.Empty ) part3 = "00";

            return part1 + part2 + part3;
      }

Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tradelineAuthor Commented:
thank you sir.
0
Fernando SotoRetiredCommented:
No problem glad I was able to help. :=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.