Move .+- inside the viewing window:
hostname (?!.+-(?:sm|sp|sa)).+
Rubular: http://www.rubular.com/r/OuSwOLHhEy
Your current expression does not work correctly, because when .+- is outside the lookahead, it can go back until lookahead no longer causes the regular expression to fail. For example, with the string hostname 9amnbb-aaa-sa01c and the regular expression hostname .+-(?!sm|sp|sa).+ , The first .+ Will match 9amnbb , lookahead will see aa as the next two characters and continue, and the second .+ woudl match aaa-sa01c .
An alternative to my current regex would be this:
hostname .+-(?!sm|sp|sa)[^-]+?$
This will prevent backtracking, since after viewing it may occur not - but not greedy ? for this to work correctly in multi-line global mode.
source share