Solved

REGULAR EXPRESSIONS in C#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
Replace &lt; with < 14 54
SQL Login 17 38
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 25
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

12 Experts available now in Live!

Get 1:1 Help Now