Home > Back-end >  Extract only rows where certain word does not exists
Extract only rows where certain word does not exists

Time:11-03

I need to extract only those rows where it starts with 8 digits and a space and then 2 digits and ignore the word TDS

Input

91298254 02/07/2021 91298254 3181753640RE,MAN-1 200,000.00
91298254 20/09/2021 TDS -577.19
91298268 03/07/2021 91298268 3181753647RE,MAN-2 166,861.00
91298268 20/09/2021 TDS -130.37

Output

91298254 02/07/2021 91298254 3181753640RE,MAN-1 200,000.00
91298268 03/07/2021 91298268 3181753647RE,MAN-2 166,861.00

I have tried \b\d{8} \d{2}.* and it gives me all rows with 8 digits space and another 2 digits.

Need you advice here.

Regards, Manjesh

CodePudding user response:

You can use

^\d{8} \d{2}(?!.*\bTDS\b).*

See the regex demo. Details:

  • ^ - start of string
  • \d{8} \d{2} - eight digits, space, two digits
  • (?!.*\bTDS\b) - a negative lookahead that fails the match if there are zero or more chars other than line break chars as many as possible followed with TDS as a whole word immediately to the right of the current location
  • .* - the rest of the line.

CodePudding user response:

This can work, to catch lines with TDS in them

(?<=^|\n).*TDS.*(?=\n|$)

Regex 101 demo

CodePudding user response:

const str =   `91298254 02/07/2021 91298254 3181753640RE,MAN-1 200,000.00
91298254 20/09/2021 TDS -577.19
91298268 03/07/2021 91298268 3181753647RE,MAN-2 166,861.00
91298268 20/09/2021 TDS -130.37`

const result = str.replace(/(^|\n)\d{8}\s \d{2}\/\d{2}\/\d{4}\s TDS[^\n]*/g, '')
console.log(result)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related