Solved

REGULAR EXPRESSIONS in C#

Posted on 2006-07-06
8
221 Views
Last Modified: 2011-09-20
Hello...could some1 plz tell me the regular expression to extract numbers from text files in C#. Number could be of any size with commas occuring at different positions. A few examples of the number formats could be as follows
500,000,000
56,000
430,064

i have tried for a week with diff expressions  and am not getting it,
thx
0
Comment
Question by:jhav1594
  • 3
8 Comments
 
LVL 15

Expert Comment

by:bpmurray
ID: 17051768
Try one of these:

1. Optional minus sign value from 0 to 999,999,999 with option decimal places
      Regex rx = new Regex(@"-?(\d{1-3},){0-2}\d{1-3}(\.\d+)?");

2. Same as the above, but with no minus sign:
      Regex rx = new Regex(@"(\d{1-3},){0-2}\d{1-3}(\.\d+)?");

3. Same as #2 but no decimals:
      Regex rx = new Regex(@"(\d{1-3},){0-2}\d{1-3}");

You use it like:

        MatchCollection matches = rx.Matches(text);
        foreach (Match match in matches)
        {
             [process the matching string]
        }


 
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17051784
Just thinking about it - this regexp will allow numbers like 123,456,7 which isn't what you want, so the expression should be:

       "((\d{1-3},){0-2}\d{3})|(\d{1.2})"
0
 

Author Comment

by:jhav1594
ID: 17052613
hey thanks...but this isnt working for me.....it didnt match up...any ideas what mite be going wrong...
thx
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 250 total points
ID: 17053261
I must have been asleep when I did that  - apologies!

Regex rx = new Regex(@"((\d{1,3},){0,2}\d{3})|(\d{1,2})");
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Email Body 4 46
Put text in a picture ASP.NET C# 2 36
Why is "packages element is not declared"? 2 37
Long URL in SMS 6 24
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now