Regex that match some given word and ignore some other word

Viewed 50

I want to write regex for match strings- For example there are below two string for match regex-

  1. 05-Jul-2021 ***IDCW Transfer plan ( IDCW TransferRegistered)Corporate Bond Fund - DP Growth***

  2. 17-Sep-2021 ***IDCW TransferOut/IDCW paid & Transferred to other scheme @ Rs.0.70 per unit to Corporate Bond Fund - DP Growth***

If string have (IDCW or Dividend), (Payout|Paid|Pay Out|Transfer) then match else if string have (Registered or register) then not match

MY Regex: /\*{3}\s*([\w\W]*(Dividend|IDCW) (Payout|Paid|Pay Out|Transfer)[\w\W&?!Registered]*)\s*\*{3}$/i

Please suggest me the right regex.

1 Answers

You can use

\*{3}\s*((?:(?!Registered|\*{3})[\w\W])*(Dividend|IDCW) (Payout|Paid|Pay Out|Transfer)(?:(?!Registered)[\w\W]|\*{3})*?)\s*\*{3}

See the regex demo.

Details:

  • \*{3} - three asterisks
  • \s* - zero or more whitespaces
  • ((?:(?!Registered|\*{3})[\w\W])*(Dividend|IDCW) (Payout|Paid|Pay Out|Transfer)(?:(?!Registered)[\w\W]|\*{3})*?) - Group 1:
    • (?:(?!Registered|\*{3})[\w\W])* - any char, zero or more but as many as possible occurrences, that does not start a Registered or *** char sequences
    • (Dividend|IDCW) - one of the strings listed
    • - a space
    • (Payout|Paid|Pay Out|Transfer) - one of the strings listed
    • (?:(?!Registered)[\w\W]|\*{3})*? - any char, zero or more but as few as possible occurrences, that does not start a Registered or *** char sequences
  • \s* - zero or more whitespaces
  • \*{3} - three asterisks
Related