删除连续数字之间的空格

时间:2019-02-26 10:17:59

标签: c# regex

我有一个字符串,我想从中删除数字之间的空格

string test = "Some Words 1 2 3 4";
string result = Regex.Replace(test, @"(\d)\s(\d)", @"$1$2");

预期/期望结果将是:

"Some Words 1234"

但是我检索到以下内容:

"Some Words 12 34"

我在这里做什么错了?

更多示例:

Input:  "Some Words That Should not be replaced 12 9 123 4 12"
Output: "Some Words That Should not be replaced 129123412"

Input:  "test 9 8"
Output: "test 98"

Input:  "t e s t 9 8"
Output: "t e s t 98"

Input:  "Another 12 000"
Output: "Another 12000"

2 个答案:

答案 0 :(得分:45)

Regex.Replace继续在上一个匹配项之后 进行搜索:

Some Words 1 2 3 4
           ^^^
         first match, replace by "12"

Some Words 12 3 4
             ^
             +-- continue searching here

Some Words 12 3 4
              ^^^
            next match, replace by "34"

您可以使用zero-width positive lookahead assertion来避免这种情况:

string result = Regex.Replace(test, @"(\d)\s(?=\d)", @"$1");

现在,最后一位不是比赛的一部分:

Some Words 1 2 3 4
           ^^?
         first match, replace by "1"

Some Words 12 3 4
            ^
            +-- continue searching here

Some Words 12 3 4
            ^^?
            next match, replace by "2"

...

答案 1 :(得分:43)

您的正则表达式消耗右边的数字。 (\d)\s(\d)1中的Some Words 1 2 3 4匹配并捕获到组1中,然后匹配1个空格,然后匹配并使用(即添加到匹配值并提高正则表达式索引){{1} }。然后,正则表达式引擎尝试从当前索引中找到另一个匹配项,该匹配项已经在2之后。因此,正则表达式与1 2不匹配,但是找到了2 3

这里是your regex demo,并显示如下图:

enter image description here

此外,请在此处查看匹配过程:

enter image description here

使用不消耗的环顾四周:

3 4

请参见regex demo

enter image description here

详细信息

  • (?<=\d)\s+(?=\d) -与字符串中的位置相匹配的正向后缀,紧跟数字之后
  • (?<=\d)-超过1个空格
  • \s+-一个正向超前查询,它与字符串中的位置立即匹配,后跟一个数字。

C#演示:

(?=\d)

请参见online demo

string test = "Some Words 1 2 3 4";
string result = Regex.Replace(test, @"(?<=\d)\s+(?=\d)", "");

输出:

var strs = new List<string> {"Some Words 1 2 3 4", "Some Words That Should not be replaced 12 9 123 4 12", "test 9 8", "t e s t 9 8", "Another 12 000" };
foreach (var test in strs) 
{
    Console.WriteLine(Regex.Replace(test, @"(?<=\d)\s+(?=\d)", ""));
}