Skip to main content
 首页 » 编程设计

c#之使用正则表达式替换

2024年01月25日29kenshinobiy

我想制作一个从字符串中换行的函数。

例如:“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" 

模式总是[数字][点][空白]。 如果第一个选项带有换行,这不是问题。

请您参考如下方法:

给这个家伙一个机会

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