如何用正则表达式获取整个部分(直到一行不是以空格开头)

时间:2019-02-05 08:15:52

标签: c# regex

我有下一个字符串:

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

我想(使用正则表达式)获取以“ Numbers:”开头的字符串,直到下一行以非空格字符开头(无此行...)。

在我的示例中,以非空格字符开头的“ Numbers:”之后的下一行是:“ Name \ Fruits \ etc:John”,所以我想得到:

  Zero
     One
   Two
    Three
  ***
  (n lines that start with one or more whitespace chars)
  ***

1 个答案:

答案 0 :(得分:1)

您可以使用此

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

Demo