Reputation: 1465
Input string:
----------------------
Test Id:
Some Id: 718489
517.1[2] g
----------------------
Expression (\d+\.\d)
captures 517.1.
What I need is to capture 517.12 (without []). Is this possible with Regular expression ?
Upvotes: 0
Views: 72
Reputation: 1271
As @Vedran stated in the comment. You cannot remove the brackets in a regex, the best you can do is to end up with two captures.
But if your able to process the response this could solve your problem:
var number = float.Parse(Regex.Match(@"(\d+\.(\d|\[|\])+)").Value.Replace("[", "").Replace("]", ""), CultureInfo.InvariantCulture.NumberFormat);
Upvotes: 3
Reputation: 626728
You can match the whole number with the bracket and then replace the [
with string.Empty
:
var input = "----------------------\r\nTest Id: \r\nSome Id: 718489 \r\n\r\n 517.1[2] g \r\n----------------------";
var rx = new Regex(@"(?>\b\d+\.\d\[\d+\b)");
var res = rx.Match(input).Value.Replace("[", string.Empty);
Output:
Note that (?>...)
atomic grouping removes the backtracking that we are not really interested in in this case.
Upvotes: 1