Reputation: 3646
Example;
X=This
Y=That
not matching;
ThisWordShouldNotMatchThat
ThisWordShouldNotMatch
WordShouldNotMatch
matching;
AWordShouldMatchThat
I tried (?<!...)
but seems not to be easy :)
Upvotes: 2
Views: 2653
Reputation: 336198
^(?!This).*That$
As a free-spacing regex:
^ # Start of string
(?!This) # Assert that "This" can't be matched here
.* # Match the rest of the string
That # making sure we match "That"
$ # right at the end of the string
This will match a single word that fulfills your criteria, but only if this word is the only input to the regex. If you need to find words inside a string of many other words, then use
\b(?!This)\w*That\b
\b
is the word boundary anchor, so it matches at the start and at the end of a word. \w
means "alphanumeric character. If you also want to allow non-alphanumerics as part of your "word", then use \S
instead - this will match anything that's not a space.
In Python, you could do words = re.findall(r"\b(?!This)\w*That\b", text)
.
Upvotes: 14