• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

Regular Expression : Grab everything to right of comma

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.

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

Ans. 13455.55

How can I do this?
0
marcon33
Asked:
marcon33
  • 2
  • 2
1 Solution
 
dunglaCommented:
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:
13455.55
0
 
Fernando SotoCommented:
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);


Fernando
0
 
Fernando SotoCommented:
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.

     m.Groups[1].Value;

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.

Fernando
0
 
dunglaCommented:
Thanks Fernando
0
 
rbvoigtCommented:
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 "*".
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now