【发布时间】:2017-12-31 14:40:50
【问题描述】:
我有这行代码:
textBox1.Text = Regex.Replace(textBox1.Text, "(?:\r\n)+", " \r\n");
它基本上在换行符之前添加了一个双空格键:
输入:
a
b
c
d
e
输出:
a //<--- Double spacebar after the 'a' character
b //<--- Double spacebar after the 'b' character
c //<--- Double spacebar after the 'c' character
d //<--- Double spacebar after the 'd' character
e
出于某些格式的目的,在此页面或 reddit 等页面上,需要在前一行中使用双换行符或双空格键,然后在其格式中换行以换行
无论如何,它正在工作,但问题是,如果您在该行之后已经有一个双空格键,它只会不断添加并导致一行有太多空格键,这是不必要的,因为您只需要 2 个即可
所以我尝试用 [^] 做一个例外,如果它之前已经有一个双空格键,则不应考虑该规则,如下所示:
(?:[^ ]\r\n)+
但它不起作用?
输入:
a
b
c //<---- Double spacebar before the line break to check if it ignores it
d
e
输出:
//
//
//
c //
//
e
为什么?怎么了?非常感谢
【问题讨论】: