The examples provided are incorrect. Most match line 4, and all match line 5. They're right that you need word boundaries and a negative lookahead, but they are missing a crucial piece.

Regex matches word boundaries before and after dots. Meaning if the third number has a decimal, the search will not run to the end of the line, will not see the final x, but will match the string.

As a solution you need to use this negative lookahead that checks after the decimal for an x: (?!\.?\d?x)

As well as wrapping the search in word boundaries: \b...\b

I tested the string below and it works.

\bRHS \d+\.?\d?x\d+\.?\d?x\d+\.?\d?(?!\.?\d?x)\b

Example: https://regex101.com/r/0nzHkN/2/

Answer from Josh Bradley on Stack Overflow
🌐
Regex Tester
regextester.com › 107904
Regex to match string NOT followed by [ - Regex Tester/Debugger
Url checker with or without http:// ... string not containing string Check if a string only contains numbers Only letters and numbers Match elements of a url date format (yyyy-mm-dd) Url Validation Regex | Regular Expression - Taha Match an email address Validate an ip address nginx test Extract String Between Two STRINGS special characters check match whole word Match or Validate phone number Match anything enclosed by square ...
Discussions

regex - Find 'word' not followed by a certain character - Stack Overflow
This regex matches word substring and (?!@) makes sure there is no @ right after it, and if it is there, the word is not returned as a match (i.e. the match fails). ... Negative lookahead is indispensable if you want to match something not followed by something else. More on stackoverflow.com
🌐 stackoverflow.com
Regex match pattern not preceded by either of two expressions

Are they actually the same length as foo and bar are - because it should work in that case.

>>> import re
>>> text = 'foo bar\nbaz bar\nblub bar'
>>> re.findall('\S+(?<!foo|baz) bar', text)
['blub bar']

The 3rd party regex module does support variable length (among other things)

>>> import regex as re
>>> re.findall('\S+(?<!foo|blub) bar', text)
['baz bar']

Alternative you could just capture all the matches and do the filtering yourself in code.

More on reddit.com
🌐 r/learnpython
5
5
September 17, 2019
python - Regular expression to search for a string1 that is never followed by string2 - Stack Overflow
2 How can I check if every substring of four zeros is followed by at least four ones using regular expressions? ... 1 A regular expression to match a keyword that does not have that same word appear before another keyword · 5 How can I find a string that contains any between two regular expressions except for a certain regex in python... More on stackoverflow.com
🌐 stackoverflow.com
regex - python 3 find a string that is not followed by a substring - Stack Overflow
I have a string that looks like this: line = "aaa farmer's blooper's mouse'd would've bbb" From my string, line, if a word ends with apostrophe or apostrophe + s ('s), then I want to drop apostrop... More on stackoverflow.com
🌐 stackoverflow.com
November 30, 2017
Top answer
1 of 1
234

The (?!@) negative look-ahead will make word match only if @ does not appear immediately after word:

word(?!@)

If you need to fail a match when a word is followed with a character/string somewhere to the right, you may use any of the three below

word(?!.*@)       # Note this will require @ to be on the same line as word
(?s)word(?!.*@)   # (except Ruby, where you need (?m)): This will check for @ anywhere...
word(?![\s\S]*@)  # ... after word even if it is on the next line(s)

See demo

This regex matches word substring and (?!@) makes sure there is no @ right after it, and if it is there, the word is not returned as a match (i.e. the match fails).

From Regular-expressions.info:

Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial explained why you cannot use a negated character class to match a q not followed by a u. Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point.

And on Character classes page:

It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". It means: "a q followed by a character that is not a u". It does not match the q in the string Iraq. It does match the q and the space after the q in Iraq is a country. Indeed: the space becomes part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp. If you want the regex to match the q, and only the q, in both strings, you need to use negative lookahead: q(?!u).

🌐
Finxter
blog.finxter.com › home › learn python blog › how to find all lines not containing a regex in python?
How to Find All Lines Not Containing a Regex in Python? - Be on the Right Side of Change
June 19, 2022 - You search for a position where neither 'hi' is in the lookahead, nor does the question mark character follow immediately. This time, we consume an arbitrary character, so the resulting match is the character 'i'. Summary: You’ve learned that you can match all lines that do not match a certain regex by using the lookahead pattern ((?!regex).)*.
🌐
Squash
squash.io › how-to-regex-regex-not-match
Using Regular Expressions to Exclude or Negate Matches
September 12, 2023 - The string.match() function returns an array of all matches, which in this case are all the word characters that are not followed by "123" in the string "Hello123World". The pipe (|) symbol can also be used to perform a "not match" operation by specifying multiple patterns separated by the pipe symbol. This allows you to match any pattern except the ones specified. For example, the regex pattern ^(?!dog$|cat$) matches any word that is not exactly "dog" or "cat".
Find elsewhere
🌐
Regular-Expressions.info
regular-expressions.info › lookaround.html
Regex Tutorial: Lookahead and Lookbehind Zero-Length Assertions
When explaining character classes, ... a q not followed by a u. Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead, we have the trivial regex ...
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex lookahead
Python Regex Lookahead
September 19, 2022 - Use the negative regex lookahead X(?!Y) that matches X only if it is not followed by Y.
🌐
O'Reilly
oreilly.com › library › view › regular-expressions-cookbook › 9780596802837 › ch05s05.html
5.5. Find Any Word Not Followed by a Specific Word - Regular Expressions Cookbook [Book]
May 22, 2009 - You want to match any word that is not immediately followed by the word cat, ignoring any whitespace, punctuation, or other nonword characters that appear in between.
Authors   Jan GoyvaertsSteven Levithan
Published   2009
Pages   510
Top answer
1 of 1
191

This should work:

/^((?!PART).)*$/

Edit (by request): How this works

The (?!...) syntax is a negative lookahead, which I've always found tough to explain. Basically, it means "whatever follows this point must not match the regular expression /PART/." The site I've linked explains this far better than I can, but I'll try to break this down:

^         #Start matching from the beginning of the string.    
(?!PART)  #This position must not be followed by the string "PART".
.         #Matches any character except line breaks (it will include those in single-line mode).
$         #Match all the way until the end of the string.

The ((?!xxx).)* idiom is probably hardest to understand. As we saw, (?!PART) looks at the string ahead and says that whatever comes next can't match the subpattern /PART/. So what we're doing with ((?!xxx).)* is going through the string letter by letter and applying the rule to all of them. Each character can be anything, but if you take that character and the next few characters after it, you'd better not get the word PART.

The ^ and $ anchors are there to demand that the rule be applied to the entire string, from beginning to end. Without those anchors, any piece of the string that didn't begin with PART would be a match. Even PART itself would have matches in it, because (for example) the letter A isn't followed by the exact string PART.

Since we do have ^ and $, if PART were anywhere in the string, one of the characters would match (?=PART). and the overall match would fail. Hope that's clear enough to be helpful.

🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.6 ...
May 25, 2026 - This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'. ... Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.