diwatu
diwatu

Reputation: 5699

Find numbers at specific positions in a string with Regex

I need a Regex pattern which is going to be used to remove some numbers at specific positions.

My string format is like this: xxxx - 2013-xxxxxx9 xxxxxxxx9, the '9' in the string means a number or doesn't exist.

The code I wrote is like this:

  string str= "dddd - 2013-0Winter1 morning2";
  Regex pattrn = new Regex(".* - ([0-9]{4}-).*([1-9]?) .*([1-9]?)$");
  Match match = pattern.Match(me);
  for (int index = match.Groups.Count - 1; index > 0; index--)
  {
    str = str.Remove(match.Groups[index].Index, match.Groups[index].Length);
  }

When I run this, match.Groups[2] and match.Groups[3]'s value are blank. But I want to find '2013','1' and '2' in the string.

The result is:

"dddd - 0Winter1 morning2";

The result I want is:

"dddd - 0Winter morning";

Does anybody know why?

Upvotes: 1

Views: 2061

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

The problem is that your .* is greedy so it's gobbling up the ([1-9]?) that comes after it. If you use a non-greedy quantifier (.*?), you'll get the result you want:

string str = "dddd - 2013-0Winter1 morning2";
Regex pattern = new Regex("^.* - ([0-9]{4}-).*?([1-9]?) .*?([1-9]?)$");
Match match = pattern.Match(str);
for (int index = match.Groups.Count - 1; index > 0; index--)
{
    str = str.Remove(match.Groups[index].Index, match.Groups[index].Length);
}

Console.WriteLine(str); // dddd - 0Winter morning

Of course, this will produce the same result:

string str = "dddd - 2013-0Winter1 morning2";
str = Regex.Replace(str, @"^(\w*? [0-9]{4}-\w*?)[1-9]? (\w*?)[1-9]?$", "$1 $2");
Console.WriteLine(str); // dddd - 0Winter morning

Upvotes: 2

Related Questions