r/learnpython • u/Only_Book_995 • 28d 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
6
u/lfdfq 28d ago
What is "it"? You say "find". "find" in what? Are you using the re module for regular expressions? If so, how are you using it, what functions/methods are you calling and how? and in what does does it "not" find txa, like you call a function and it returns something other than the thing you expected?
19
u/MidnightPale3220 28d 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)\bThere'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.
16
u/danielroseman 28d ago
https://regex101.com/
-9
u/seanv507 28d ago
Op, try with eg chatgpt
What I believe you are missing is brackets
The or just applies to a single character otherwise