用于匹配多行文本的正则表达式

Regex for matching multiline text

我有一个多行文本,我想匹配单词“描述金额”和一个随机数字之间的文本,并以逗号作为分隔符。
示例输入:

Description Amount
Being the Amount received from above branch in favour of SAF SAF
FOR THE MONTH OF JANUARY-2021.
3,24,500.00

预期输出:

Being the Amount received from above branch in favour of SAF SAF
FOR THE MONTH OF JANUARY-2021.

示例输入:

Description Amount
Being the Amount received from above branch in favour of SD SAF
VALUE SAF OF CHAPAINAWABGANJ AREA FOR THE MONTH OF
JANUARY 2021
9,18,049.00

预期输出:

Being the Amount received from above branch in favour of SD SAF
VALUE SAF OF CHAPAINAWABGANJ AREA FOR THE MONTH OF
JANUARY 2021

输入包含换行符的多个变量编号。我使用了以下正则表达式:

(?<=Description Amount\s)(.*\n.*)

但需要提前improvement.Thanks。

如果数字在行首,您可以使用

(?<=Description Amount\n)(?:(?!\d+,\d).*\n)*

模式匹配

  • (?<=Description Amount\n)
  • (?:非捕获组
    • (?!\d+,\d).*\n 如果不是以数字、逗号和数字开头,则匹配整行
  • )* 关闭群组并可选择重复

Regex demo

如果应该至少有一行,您可以将组的量词更改为+

(?<=Description Amount\n)(?:(?!\d+,\d).*\n)+

Regex demo

您可以试试这个正则表达式,它避免了前瞻的多重断言:

(?<=Description Amount\n)(?:.*\n)+?(?=\d+,\d)

RegEx Demo

正则表达式详细信息:

  • (?<=Description Amount\n):断言我们在当前位置
  • 之前的一行中有Description Amount
  • (?:.*\n)+?: 匹配任意文本的1行或多行
  • (?=\d+,\d):断言我们在当前位置
  • 之前有number,digit