CodeHard
CodeHard

Reputation: 125

Python RegEx Stop before a Word

I have a bit of RegEx I am trying to figure out: ( of [A-Za-z ]+)?

The above portion of my Regex will match the following:

of New Mexico and Mrs Smith.

What I am trying to do is have the RegEx stop before and.

( of [A-Za-z ]+)\sand?

The above RegEx is very close to solving the issue however it still matches and.

The above matches:

of New Mexico and

I want it to output:

of New Mexico

Upvotes: 2

Views: 2184

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626709

You can use a tempered greedy token:

( of (?:(?!\band\b)[A-Za-z ])+)?
     ^^^^^^^^^^^^^^^^^^^^^^^^^

See the regex demo

The (?:(?!\band\b)[A-Za-z ])+ construct matches 1+ characters defined in the [A-Za-z ] character class that are not a whole word and.

Python demo:

import re
p = re.compile(r'( of (?:(?!\band\b)[A-Za-z ])+)?')
s = " of New Mexico and Mrs Smith."
m = p.search(s)
if m:
    print(m.group().strip())

Upvotes: 5

Related Questions