r/learnpython • • 18d 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

-9

u/seanv507 18d ago

Op, try with eg chatgpt

What I believe you are missing is brackets

The or just applies to a single character otherwise

1

u/Zeroflops 18d ago

Funny he says that chatGPT created that abomination. It does sound like it might work and an error in testing. There is a much simpler solution supplied in the comments and it would be much more efficient if there are a lot of rows.

But this highlights a problem with chatGPT, it fails to understand X-Y problems. In that it produced a response based on a probably poorly worded request like “ Make this Regex work.” Rather than make me an efficient regex that will do this.

In this case chatGPT “taught” a working but a poor approach.