简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM