Solved

REGULAR EXPRESSIONS in C#

Posted on 2006-07-06
8
223 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 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

10 Experts available now in Live!

Get 1:1 Help Now