?
Solved

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

Posted on 2006-04-24
5
Medium Priority
?
152 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:tradeline
  • 3
  • 2
5 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16527484
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
 

Author Comment

by:tradeline
ID: 16539221
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16540436
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
 

Author Comment

by:tradeline
ID: 16545671
thank you sir.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16547612
No problem glad I was able to help. :=)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question