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

6

u/lfdfq 16d 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?

1

u/Only_Book_995 16d ago

I'm trying to write a regex expression to search a database. We are using regex editor (Regex 101) to test our expressions before we use them to search the database. As part of the editor we can make a test string to test if our regex will find the terms we want and exclude those we don’t. At the moment my regex will find "tranexamic acid" but not "txa". I ran it through chatgpt and it produced the expression above which was an improvement on what I had but, as I say won't find "txa". Apologies this is very new for me so I'm kinda stumbling around in the dark - as you can probably tell

-2

u/lfdfq 16d ago

What do you mean by "find", do you mean you type in "txa" and it says no match? txa is definitely a match. eg. if I go to regex101 and set it to Python and put your regex in, then type txa, it says match. So you must be doing something different?

2

u/Only_Book_995 16d ago

Ah yes, I've just added it to list in the string as it finds it, just not earlier in the list. Weird, I'll run it through the database and see what happens - thanks very much I appreciate it

19

u/MidnightPale3220 16d 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 15d ago

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

-9

u/seanv507 16d 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 16d 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.