Posted on 2006-07-06
Medium Priority
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

i have tried for a week with diff expressions  and am not getting it,
Question by:jhav1594
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 15

Expert Comment

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]

LVL 15

Expert Comment

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:


Author Comment

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

Accepted Solution

bpmurray earned 1000 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})");

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

777 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