Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用正则表达式将多减号替换为一减号_C#_Regex_Replace - Fatal编程技术网

C# 用正则表达式将多减号替换为一减号

C# 用正则表达式将多减号替换为一减号,c#,regex,replace,C#,Regex,Replace,有一根绳子 str = "test--removing-----minus-"; 试图用 str = Regex.Replace(str, @"\-*", @"-"); 但我明白了 "t-e-s-t--r-e-m-o-v-i-n-g--m-i-n-u-s-"; *匹配上一个模式的0个或多个匹配项。使用{2,}匹配2个或多个匹配项 和-不需要逃脱 str = Regex.Replace(str, @"-{2,}", @"-"); 你的代码是 str = Regex.Replace(str,

有一根绳子

str = "test--removing-----minus-";
试图用

str = Regex.Replace(str, @"\-*", @"-");
但我明白了

"t-e-s-t--r-e-m-o-v-i-n-g--m-i-n-u-s-";
*匹配上一个模式的0个或多个匹配项。使用{2,}匹配2个或多个匹配项

和-不需要逃脱

str = Regex.Replace(str, @"-{2,}", @"-");
你的代码是

str = Regex.Replace(str, @"\-+", @"-");

*使正则表达式贪婪,它表示零次或多次。因此,它将零次出现。使用{1,}匹配一个或多个字符-

问题是*匹配零个或多个字符,这意味着它在字母之间匹配


将其更改为+表示一个或多个。

我发现很难相信在outputCopy粘贴错误中添加了字母b。谢谢修正!:我的第一个想法是只说一个或多个会更简洁,但后来我意识到这可能会使正则表达式变成-+。。。此外,字符串前面也不需要@。
str = Regex.Replace(str, @"\-{1,}", @"-");