Regular expression after character

I am trying to use a regular expression to get the value after a character. In this case, the string is m325 , and I need to get everything that is after m .

Can someone 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 regular expression "m (. +) $" Returns m, as well as the string I need. I tried the β€œGroups” example and it returns the data I want. Why do I need to use groups?

+4
source share
7 answers

Besides the missing one ) , you have simplified it a bit. You need to do

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

(You might want to add a check if Match() matches or not.)

+5
source

You do not have enough closure). In addition, if you are extracting a number, you should limit yourself to only numbers, avoid trying to parse the erroneous string into an integer in the following expression.

 m(\d*) 
+2
source

"m (. +) $" - was not closed (

+1
source

Required regex /^m(.*)$/

In fact, you should use \d or [0-9] if you need matching numbers.

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

and

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

if there are always 3 digits

+1
source

Alternatively, you can check it out at: http://gskinner.com/RegExr/

What values ​​may appear after the character "m"? If it is only an integer, I should use the Shekhar_Pro solution. If any character, go with the rest :)

+1
source

Looks like you missed the close )

0
source

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

0
source

Source: https://habr.com/ru/post/1338398/


All Articles