c# - 识别时换行(数字+字符串)

标签 c# .net regex

我有以下几句话:

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+
enter image description here
我在 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/

    相关文章:

    c# - 在 WinRT 应用程序中处理 2、3、4、5 个手指敲击、双击和按住手势

    c# - 读取配置部分的通用方法

    python - 如何捕捉一组最长的序列

    c# - 如何发出静态外部方法?

    c# - 单声道 ASP.NET 异常

    c# - 如何阻止选择标签助手自动添加 'multiple' 属性

    c# - 使用 JSON.NET 从 JSON 中获取值(value)

    html - 替代具有可变宽度的后视

    regex - 在 VB.Net 源代码中获取注释的正则表达式

    c# - 在 Unity 中从一个程序向另一个程序发送消息