如何获得与正则表达式的整段(直到行不以空格开头)

问题描述 投票:-2回答:1

我有下一个字符串:

Numbers: Zero
     One
   Two
    Three
  ***
  (n lines that start with one or more whitespace chars)
  ***
Name\Fruits\etc: John
  Jane

我想(与正则表达式),其开头的字符串“号:”直到与非空白字符开头的下一行(没有这条线......)。

我在我的例子中,后的下一行的“数字”与非空白字符开头是:“名称\水果\等:约翰”,所以我想:

  Zero
     One
   Two
    Three
  ***
  (n lines that start with one or more whitespace chars)
  ***
c# regex
1个回答
1
投票

您可以使用此

^(?:Numbers:)([\w\W]+?)(?=^\S)
  • ^ - 锚开始的字符串。
  • (?:数字:) - 非捕获组,匹配Numbers:
  • ([\w\W]+?) - 匹配任何东西。 (懒惰模式)。
  • (?=^\S) - 后面必须有非空格字符换行。

Demo

© www.soinside.com 2019 - 2024. All rights reserved.