开发者

Regex after character

开发者 https://www.devze.com 2023-02-09 01:49 出处:网络
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.

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 r开发者_JAVA技巧equire. I have tried the Groups example and it returns the data that I want. Why do I need to use Groups to do this?


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.)


You are missing a closing ). Plus, if you are extracting a number you should limit yourself to digits only, Will avoid trying to parse a faulty string into integer in the next statement.

m(\d*)


"m(.+)$" - there wasn't closed (


Also, you can test it on: http://gskinner.com/RegExr/

What values can appear behind the "m" character? If it's only an integer, the I should use the solution Shekhar_Pro provided.. If any character, go with the rest :)


The regex you require is /^m(.*)$/

Actually you should use \d or [0-9] if you want match digits.

/^m([0-9]*)$/ 

and

/^m([0-9]{3})$/

if there are always 3 digits


Looks like you missed the closing )


A simple "m\d*" should do this.. please show us whole string to see the case.

0

精彩评论

暂无评论...
验证码 换一张
取 消