我有以下几句话:
12"The world... 13The end 14"I have 16 years 15Kiss me
我需要像这样分开它:
12"The world...
13The end
14"I have 16 years
15Kiss me
要分开,我使用
(number + string)
示例 (123xxx)
任何与我需要的字符串连接的数字 break
\n
前。我使用了下面的正则表达式,但是没有考虑到特殊符号,文中会有几个符号。甚至自己选择一个数字
\d+\w+
我在 C# 中使用此代码,我只需要输入一个有效的正则表达式:
private void method()
{
string text = "12\"The world... 13The end 14\"I have 16 years 15Kiss me";
string ntext = Regex.Replace(text, @"\d+\w+", "\n");
}
我怎样才能制作一个需要 (number + string)
的正则表达式并排成一行 break
?
最佳答案
您可以使用
Regex.Replace(text, @"\s+(?=\d+[^\s\d])", "\n")
见 regex demo .详情
\s+
- 一个或多个空白字符 (?=\d+[^\s\d])
- 正前瞻匹配紧跟在 1+ 位数字之后的位置,然后是除数字和空格以外的数字。 见 C# demo :
var text = "12\"The world... 13The end 14\"I have 16 years 15Kiss me";
Console.WriteLine(Regex.Replace(text, @"\s+(?=\d+[^\s\d])", "\n"));
输出:12"The world...
13The end
14"I have 16 years
15Kiss me
关于c# - 识别时换行(数字+字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64949126/