I am working with C# and .NET. The strings I am looking at have are text, possibly with numbers in various places and an optional final question mark.
If there is a number at the end of the string or before the question mark I need to capture it, parse it, and then remove it from the string. Here is my code:
Match trailingNumber = Regex.Match(command, @"\d+(?:\??)$");
if (!String.IsNullOrEmpty(trailingNumber.Value) && int.TryParse(trailingNumber.Value, out index))
command = Regex.Replace(command, @"\d+(\??)$", "$1");
The problem is that the non-capturing group is capturing the '?' in a string such as "3abc99?" causing the match to be "99?" instead of "99". In the replace command I am able to deal with this but how can I prevent the initial match from including the question mark?
Thanks!