2012-04-04 107 views
1

我想制作一个函数,可以从字符串中断行。使用正则表达式替换

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

字符串该模式将始终为[数字] [点] [空格]。 如果第一个选项带有新行,这不是问题。

+0

所以仅仅是明确的(因为它似乎目前的答案都忽略了这):你**不需要''''前面​​的CRLF? – slugster 2012-04-04 00:15:42

回答

4

给这家伙一个镜头

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

完美的作品!谢谢 :) – 2012-04-04 01:14:18

2
Regex rgx = new Regex("(\\d+\\.\\s)"); 
String replaced = rgx.Replace(Input, Environment.NewLine + "$1"); 
1

短一点表情像这样的工作太:

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