简体   繁体   中英

Regex for matching multiline text

I have a multi line text from where i want to match the text between the word "Description Amount" and a random digit with commas as delimiter.
Example Input:

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

Expected Output:

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

Example input:

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

Expected Output:

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

The input contains multiple variable no of line breaks. I used the following regex:

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

But it needs improvement.Thanks in advance.

If the digit is at the start of the line, you might use

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

The pattern matches

  • (?<=Description Amount\n)
  • (?: Non capture group
    • (?,\d+.\d).*\n Match the whole line if it does not start with digits, a comma and a digit
  • )* Close the group and optionally repeat

Regex demo

If there should be at least a single line, you can change the quantifier of the group to +

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

Regex demo

You may try this regex that avoids multiple assertions of lookahead:

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

RegEx Demo

RegEx Details:

  • (?<=Description Amount\n) : Assert that we have Description Amount in a line before the current position
  • (?:.*\n)+? : Match 1 or more lines of any text
  • (?=\d+,\d) : Assert that we have number,digit ahead of the current position

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM