Solved

REGULAR EXPRESSIONS in C#

Posted on 2006-07-06
8
225 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
[X]
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
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

Independent Software Vendors: 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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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