Why does the regex return one digit?
I want to get the last digits from strings.
For example: "Text11" - 11
; "Te1xt32" - 32
etc.
I am writing this regex:
var regex = new Regex(@"^(.+)(?<Number>(\d+))(\z)");
And use it:
regex.Match(input).Groups["Number"].Value;
This returns 1
for "Text11" and 2
instead of "Te1xt32" instead of 11
and 32
.
So the question is, why \d+
only get the last digit?
source to share
Since the .+
default is greedy by default, so .+
greedily matches the last one and then goes back to the previous character and uses the \d+
inorder pattern to create a match. You need to add a non-greedy quantifier ?
next to +
to force the regex engine to do the unwanted match or the shortest possible match.
var regex = new Regex(@"^(.+?)(?<Number>(\d+))(\z)");
source to share
Alternatively, you can use the same regex in the RightToLeft
mode:
var input = "Te1xt32";
// I removed some unnecessary capturing groups in your regex
var regex = new Regex(@"^(.+)(?<Number>\d+)\z", RegexOptions.RightToLeft);
// You need to specify the starting index as the end of the string
Match m = regex.Match(input, input.Length);
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(m.Groups["Number"].Value);
Since what you want to find is at the end of the line and the part in front has no pattern, going from right to left avoids some indentation in this case, although the difference, if any, would be negligible in that case.
RightToLeft
, as the name suggests, does a right-to-left match, so the numbers at the end of the string will be eagerly consumed \d+
until the rest are consumed .+
.
source to share