Regular Expression : Grab everything to right of comma

Posted on 2006-04-25
Last Modified: 2007-12-19
I want to use regular expressions in to grab everything to the right of the last comma. I realized using string functions is a way to do this but I want to use regular expressions.

The number of characters will be varying and in some cases there may be a decimal point and cents left over.

"M111        ,11/1/2005 0:00:00,13455.55";

Ans. 13455.55

How can I do this?
Question by:marcon33
    LVL 13

    Expert Comment

    Hi marcon33,

    Try this one
    string pattern = @"\b(,)\b[0-9]?.+";
    string content = "M111        ,11/1/2005 0:00:00,13455.55";
    Match m = Regex.Match(content, pattern, RegexOptions.IgnoreCase);
    if (m.Success)
          Console.WriteLine(m.Value.Replace(",", string.Empty));

    // result:
    LVL 62

    Accepted Solution

    Hi marcon33;

    For strings formated the way the example was given this will work.

    using System.Text.RegularExpressions;

          string input = "M111        ,11/1/2005 0:00:00,13455.55";
          string LastValue = String.Empty;
          Match m = Regex.Match(input, @":\d\d\s*?,\s*?(\d+\.?\d*)");
          LastValue = m.Groups[1].Value;
          MessageBox.Show("Last value is = " + LastValue);

    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi dungla;

    You could modify your pattern so that you do not have to do a replace on the string returned from Regex. You can change the pattern to "\b,\b([0-9]?.+)" and then to get the results of the match you can do it like this.


    The other thing about the pattern you are using is that it matches only the first digit and excepts any character after it. You can change your pattern to this "\b,\b([0-9]+\.?[0-9]*)" only to except digits and a decimal point.

    LVL 13

    Expert Comment

    Thanks Fernando
    LVL 1

    Expert Comment

    In perl I would use

    I think the .NET regex syntax is very close... you want to anchor to the end of the string "$", then include all characters except comma "[^,]", as many times as possible "*".

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    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…
    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.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    737 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

    23 Experts available now in Live!

    Get 1:1 Help Now