Question

I'm writing a Perl script that performs different actions on hosts depending on which patterns match the FQDN. I've been struggling to find a regular expression that skips hosts that have the string 'test' in the domain name.

These host names represent the four host name types I'm dealing with:

  • node01.prod.com
  • node01.test.com
  • node02.dmz.prod.com
  • node02.dmz.test.com

The following expression matches the host name pattern I'm trying to skip:

/\w\.test/

But, none of the negative look-ahead expressions I've tried will skip the host names with 'test'. For example, this expression:

/\w\.(?!test)/

matches/passes all four host name types, including the two that contain the string 'test'.

What's really driving me crazy is that if I hard code part of the host name, the negative look-ahead expression does skip the full host name:

/node01\.(?!test)/    # only matches node01.prod.com

I'm surely missing something terribly obvious - any suggestions?

Was it helpful?

Solution

The problem is that you're putting the negative lookahead after your match, which allows it to match a partial node name even if it has the word test in it somewhere.

This expression will match any string that doesn't contain test:

(?!.*test)^.*$

Online demonstration:

http://regex101.com/r/rZ0vO2

OTHER TIPS

You can use this regex:

/\w\.(?!test).+/

Your negative lookahead is correct but your regex is not really matching anything after dot.

Something like the following should do it:

/(?:\w+\.(?!test))+\w+/

Depending on how you are using the regex, you may also need some anchors to prevent this from matching after test:

/^(?:\w+\.(?!test))+\w+$/

This works by putting the negative lookahead within a repeating group, so that it is checked after each . that is matched in your regex.

For example: http://rubular.com/r/TeUYi9EIEL

Did you try this pattern?

/(?!.*\.test\.)^.+/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top