C# 使用正则表达式替换

C# 使用正则表达式替换,c#,regex,C#,Regex,我想做一个函数,从字符串中断开行 例如:“1。”->“\n1” 所以我可以写这样的代码 string Input = "1. First option"; Input += "2. Second option"; Input += "3. Third option"; Output = WriteMenu(Input); "1. First option \n2. Second option \n3. Third option" 拿一根这样的绳子 string Input = "1. Fi

我想做一个函数,从字符串中断开行

例如:“1。”->“\n1”

所以我可以写这样的代码

string Input = "1. First option";
Input += "2. Second option";
Input += "3. Third option";

Output = WriteMenu(Input);
"1. First option
\n2. Second option
\n3. Third option"
拿一根这样的绳子

string Input = "1. First option";
Input += "2. Second option";
Input += "3. Third option";

Output = WriteMenu(Input);
"1. First option
\n2. Second option
\n3. Third option"
模式将始终为[数字][点][空白]。 如果第一个选项是新的线路,这不是问题。

给这家伙一个机会

Input = Regex.Replace(Input, @"(?<!^)(\d+\.)", "\n$1")
Input=Regex.Replace(Input,@)(?给这个家伙一个机会

Input = Regex.Replace(Input, @"(?<!^)(\d+\.)", "\n$1")

Input=Regex.Replace(Input,@)(?像这样稍微短一点的表达式也可以:

Regex.Replace(Input, @"(?!^)\d+\.", "\n$0")

像这样稍微短一点的表达式也可以:

Regex.Replace(Input, @"(?!^)\d+\.", "\n$0")
因此,为了明确起见(因为当前的答案似乎忽略了这一点):您不希望在
1.
前面有一个CRLF。因此,为了明确起见(因为当前的答案似乎忽略了这一点):您不希望在
1.
前面有一个CRLF?