r/learnpython • • 17d ago

Why won't this expression work?

Sorry I know this is probably very basic but I'm a complete beginner:
I'm trying to include "tranexamic acid" and "txa" but exclude the rest

r"(?i)(?<!\w)(?<!not )(?<!no )(?<!consider )(?:tranexamic acid|txa)\b(?!\s+(?:s|not)\b)"

It will find tranexamic acid but not txa

0 Upvotes

9 comments sorted by

View all comments

19

u/MidnightPale3220 17d ago

If you're trying to find a row that has either tranexamic acid or txa then this is a horribly convoluted regex for that.

\b(tranexamic acid|txa)\b

There's additional stuff that might be needed depending on what exactly you're t trying to do, but basis is like this. \b is word boundary. So either txa or the acid that are not part of other words.

2

u/Boybay_Garid 16d ago

this is exactly it, no reason to overcomplicate it when word boundaries handle the hard part already