Vlad
Vlad

Reputation: 1439

Why does regex return one digit?

I want to get last digits from strings.

For example: "Text11" - 11; "Te1xt32" - 32 and etc.

I write this regex:

var regex = new Regex(@"^(.+)(?<Number>(\d+))(\z)");

And use it:

 regex.Match(input).Groups["Number"].Value;

That returns 1 for "Text11" and 2 for "Te1xt32" instead 11 and 32.

So question, Why \d+ get only last digit?

Upvotes: 3

Views: 232

Answers (3)

Toto
Toto

Reputation: 91385

You can simply do:

var regex = new Regex(@"(?<Number>\d+)\z");

Upvotes: 1

nhahtdh
nhahtdh

Reputation: 56809

As an alternative, you can use the same regex with in 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);

Demo on ideone

Since what you want to find is at the end of the string and the part in front doesn't have any pattern, going from right to left avoids some backtracking in this case, though the difference, if any, is going to be insignificant in this case.

RightToLeft mode, as the name suggests, performs the match from right to left, so the numbers at the end of the string will be greedily consumed by \d+ before the rest is consumed by .+.

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

Because .+ at the first is greedy by default, so .+ matches greedily upto the last and then it backtracks to the previous character and uses the pattern \d+ inorder to produce a match. You need to add a non-greedy quantifier ? next to the + to make the regex engine to do a non-greedy match or shortest possible match.

var regex = new Regex(@"^(.+?)(?<Number>(\d+))(\z)");

DEMO

Upvotes: 2

Related Questions