Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular Expression to remove a specific word which is not followed by a space

WFH

STOPPED AT 2023 02 21 13 08 31

DURATION 01 50 56

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NUMBER OF PARTICIPANTS 1

PARTICIPANTS

I have this problem statement where I want to remove the PARTICIPANTS which is on new line but doing so it removes the other PARTICIPANTS i.e.’NUMBER OF PARTICIPANTS 1′ this one. I don’t want that.

I have tried many regex but failed to find the desired the output. Either both of them get removed or none.

Help is really appreciated.

Thankyou in advance

>Solution :

You can use the multiline flag so that the ^ and $ anchors match at the start and end of each line. This way, you can replace the string "PARTICIPANTS" only when it’s on its own line.

re.sub(r"(?m)^PARTICIPANTS$", "", your_str)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading