正则表达式以在字符串开头的每个短线前添加空格。

huangapple go评论58阅读模式
英文:

Regular Expression to add space to each dash at the beginning of string

问题

以下是您要的翻译结果:

var input = "---Three dashes a-b-c-d";

通过正则表达式获取

var output = "- - - Three dashes a-b-c-d";

并且通过正则表达式将其还原,删除开头的额外空格。output => input

不使用正则表达式的代码 输入 => 输出

var input = "---Three dashes a-b-c-d";
var output = "";
var i = 0;
while (i < input.Length && input[i] == '-')
{
    output += "- ";
    i++;
}
output += input.Substring(i);
英文:
var input = &quot;---Three dashes a-b-c-d&quot;;

Regex to get

var output = &quot;- - - Three dashes a-b-c-d&quot;; 

and Regex get back to original, added spaces are deleted at the beginning. output => input

Code without Regex input => output

var input = &quot;---Three dashes a-b-c-d&quot;;
var output = &quot;&quot;;
var i = 0;
while (i &lt; input.Length &amp;&amp; input[i] == &#39;-&#39;)
{
    output += &quot;- &quot;;
    i++;
}
output += input.Substring(i);

答案1

得分: 3

@TedLyngmo的解决方法虽然有效,但在回溯模式中使用通配符效率较低。

更高效的方法是使用正向回溯模式来确保只有短横线在匹配之前:

@(?<=^-+)

然后用空格替换匹配项。

在regex101上,上述正则表达式运行时间为0.2毫秒,而@TedLyngmo的运行时间为19.5毫秒。

演示链接:https://regex101.com/r/6uy1K8/3

用于移除添加的空格以恢复原始文本的正则表达式(带有尾随空格):

@(?<=(?:- )*-) 

然后将匹配项替换为空。

在regex101上,上述正则表达式运行时间为0.3毫秒,而@TedLyngmo的运行时间为28.1毫秒。

请注意,所需的行为需要支持可变宽度回溯的正则表达式引擎,幸运的是,C#恰好支持这一功能。否则,上述正则表达式在不支持此功能的平台上无法使用。

英文:

While @TedLyngmo's solution works, it is rather inefficient with a wild card in the lookbehind pattern.

A more efficient approach would be to use a positive lookbehind pattern to assert that only dashes precede a match:

@&quot;(?&lt;=^-+)&quot;

And substitute the match with a space.

On regex101, the above regex took 0.2ms to run while @TedLyngmo's took 19.5ms.

Demo: https://regex101.com/r/6uy1K8/3

Regex (with a trailing space) to remove the added spaces to get back the original:

@&quot;(?&lt;=^(?:- )*-) &quot;

<sup>Note the space at the end</sup>

And substitute the match with nothing.

On regex101, the above regex took 0.3ms to run while @TedLyngmo's took 28.1ms.

Demo: https://regex101.com/r/fBOirM/3

Note that the desired behaviors require a regex engine that supports variable-width lookbehind, which C# happens to have. The above regexes are otherwise not portable to platforms that do not have such a support.

答案2

得分: 1

代码翻译如下:

一种方法是使用负回顾断言来检查破折号前面没有字符,只有破折号之前有空格:

@&quot;(?&lt;![^-].*)-&quot;
  • (?&lt;! - 开始负回顾断言
    • [^-] - 除了破折号之外的任何字符
    • .* - 零个或多个任何字符
  • ) - 结束负回顾断言
  • - - 字面上的破折号

全局匹配并替换为- (破折号 + 空格)

示例


要去掉添加的空格以恢复原始文本,可以使用类似的方法:

@&quot;(?&lt;![^- ].*)- &quot;

全局匹配并替换为-(破折号)。

示例

英文:

One way could be a negative lookbehind to check that no character before a dash to add a space too is anything but a dash:

@&quot;(?&lt;![^-].*)-&quot;
  • (?&lt;! - start of negative lookbehind
    • [^-] - any character but -
    • .* - zero or more of any character
  • ) - end of negative lookbehind
  • - - a literal -

Match globally and substitute with - (dash + space)

Demo


Removing the added spaces to get the original back could be done in a similar way:

@&quot;(?&lt;![^- ].*)- &quot;

Match globally and substitute with - (dash).

Demo

答案3

得分: 1

在C#中,您可以使用\G锚点来断言在先前匹配的结束位置或字符串的开头位置:

\G-

在替换部分使用-,或者您可以使用完整的匹配,后跟一个空格,例如$0

查看 .NET正则表达式演示C#演示

var input = "---三个短线 a-b-c-d";
string result = Regex.Replace(input, @"\G-", "- ");
Console.WriteLine(result);

输出:

- - - 三个短线 a-b-c-d

如果您想将其更改回原始状态,您可以再次匹配空格:

\G-

在替换部分只使用-

查看另一个 .NET正则表达式演示

var input = "- - - 三个短线 a-b-c-d";
string result = Regex.Replace(input, @"\G- ", "-");
Console.WriteLine(result);

输出:

---三个短线 a-b-c-d
英文:

In C# you can use the \G anchor to assert the position at the end of the previous match or at the start of the string:

\G-

In the replacement use - or you can use the full match followed by a space like $0

See a .NET regex demo and a C# demo.

var input = &quot;---Three dashes a-b-c-d&quot;;
string result = Regex.Replace(input, @&quot;\G-&quot;, &quot;- &quot;);
Console.WriteLine(result);

Output

- - - Three dashes a-b-c-d

<hr>

If you want to change it back to the original, you can add matching the space again:

\G-

In the replacement use just -

See another .NET regex demo.

var input = &quot;- - - Three dashes a-b-c-d&quot;;
string result = Regex.Replace(input, @&quot;\G- &quot;, &quot;-&quot;);
Console.WriteLine(result);

Output

---Three dashes a-b-c-d

huangapple
  • 本文由 发表于 2023年3月7日 08:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定