Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex after character

Tags:

c#

regex

I am trying to use a regex to get the value after a character. In this case, the string is m325 and I need to get whatever is after the m.

Can anybody tell me what is wrong with my code?

Regex rgMeter = new Regex("m(.+$");
intMeterID = Convert.ToInt32(rgMeter.Match(strID));

Update:

Thanks for all your answers...for some reason the regex "m(.+)$" is returning the m as well as the string I require. I have tried the Groups example and it returns the data that I want. Why do I need to use Groups to do this?

like image 394
user517406 Avatar asked Feb 04 '11 09:02

user517406


1 Answers

Apart from the missing ), you oversimplified it a bit. You need to do

Regex rgMeter = new Regex("m(.+)$");
intMeterID = Convert.ToInt32(rgMeter.Match(strID).Groups[1].Value);

(Possibly, you might want to add a check if the Match() matched or not.)

like image 119
Mormegil Avatar answered Nov 15 '22 00:11

Mormegil