Search code examples
pythonregexsearchnltktextblob

Python searching for two words regex


I'm trying to find if a sentence contains the phrase "go * to", for example "go over to", "go up to", etc. I'm using Textblob, and I know I can just use below:

search_go_to = set(["go", "to"])
go_to_blob = TextBlob(var)
matches = [str(s) for s in go_to_blob.sentences if search_go_to & set(s.words)]
print(matches)

but that would also return sentences like "go over there and bring this to him", which I don't want. Anyone know how I can do something like text.find("go * to")?


Solution

  • Try to use:

    for match in re.finditer(r"go\s+\w+\s+to", text, re.IGNORECASE):