Reputation: 680
I have the following phrases:
I need to retrieve only Smith
from this phrases. I tried thousands of variants. I stoped on
(?!Mr|MrS)([^"]+)
.
Help, please.
Upvotes: 0
Views: 327
Reputation: 163372
The pattern (?!Mr|MrS)([^"]+)
asserts from the current position that what is directly to the right is not Mr or MrS and then captures 1+ occurrences of any char except "
So it will not start the match at Mr
but it will at r
because at the position before the r
the lookahead assertion it true.
Instead of using a lookaround, you could match either Mr or MrS and capture what is in between double quotes.
\mMrS? "([^"]+)"
\m
A word boundaryMrS?
Match Mr
with an optional S
"
Match a space and "
([^"]+)
capture in group 1 what is between the "
"
Match "
See a postgresql demo
For example
select REGEXP_MATCHES('Mr "Smith"', '\mMrS? "([^"]+)"');
select REGEXP_MATCHES('MrS "Smith"', '\mMrS? "([^"]+)"');
Output
regexp_matches
1 Smith
regexp_matches
1 Smith
Upvotes: 1