Link to home
Start Free TrialLog in
Avatar of marcon33
marcon33

asked on

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?
Avatar of dungla
dungla
Flag of Viet Nam image

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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks Fernando
Avatar of rbvoigt
rbvoigt

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 "*".