I am wondering how to write a regex pattern to find strings in which any word in a list is not proceeded by another word:
To give context, imagine two lists of words:
Parts = ['spout', 'handle', 'base']
Objects = ['jar', 'bottle']
Imagine the following strings
string = 'Jar with spout and base'
string2 = 'spout of jar'
string3 = 'handle of jar'
string4 = 'base of bottle with one handle'
string5 = 'bottle base'
I want to write a rule so that if we have an expression like "spout of jar" or "handle of bottle" or "bottle base", I can output a statement like "object is fragment of jar, has part spout/base" into a dataframe but if we have an expression like "jar with spout", I can output an expression like "object is jug, has part spout".
Basically, I want to write a rule so that if any word in Parts is in the string, we write that the object is a fragment--unless the word is proceeded by 'with'.
So I wrote this, with negative lookbehind followed by .* followed by any word in Parts:
rf"(?!with)(.*)(?:{'|'.join(Part)})"
But this just does not seem to work: "jar with spout" will still match this pattern when I try it in Python.
So I am just not sure how to write a regex pattern to exclude any expression involving 'with' followed by any sequence of characters, followed by a word in Parts
Super grateful for any help that can be provided here!