Home > other >  Match using particular words and suppress the text if found set of negative terms using Regex python
Match using particular words and suppress the text if found set of negative terms using Regex python

Time:09-29

Example:

Text1: Receipt and drafting of correspondence from Mr. Sharma re: availability for settlement conference.

Text2: Receipt and review of correspondence from Mr. Sharma re: availability for settlement conference.

Question: If a line contains words like "attend%","confer%" or "call%" anywhere in the line then it should find a match but if the same line also contains word like "check%" and "draft%" anywhere in that line then it should ignore that line.

Hence, Text 1 should not match since it contains word "drafting", while Text should match because it does not contain the suppression terms like check% and draft%

Tried this :(\battend|\bconfer|\bcall)(?!.*?\b(check|draf)).*

I need single line regex solution in python for this. Anyone would like to help me here?

CodePudding user response:

You should place your negative lookahead assertion at the beginning of a line instead:

^(?!.*(?:\bcheck|\bdraf)).*(\battend|\bconfer|\bcall)

Demo: https://regex101.com/r/EtBSX4/1

CodePudding user response:

This expression does what you look for:

(?=(^(?:(?!\b(draft|check)).)*$))(?=.*?(\battend|\bconfer|\bcall))

you can use positive lookahead ( ?= ) to first check if whole line does not contain the filter words than look if your desired words exist.

  • Related