Search code examples
regexpcre

Regex Need to Match word but exclude other words in any order


I have the following combination of lines:-

WAN-bridge
bridge-WAN
WAN-VLAN
ether1-WAN        <-----
ether2-hello
ether2-wan2        <-----
WAN-BRIDGE
wan-bridge
bridge-wan
vlan918-WAN
VLAN-wan
wan-ether1        <-----
wan-Bridge

I need a PCRE regex to match any line that contains 'wan' but excludes the words 'vlan' and 'bridge' in any order and irrespective of case.

I have marked the lines I wish to match.

I have tried so many variations, but none have worked.

Any help would be appreciated.


Solution

  • You can use this

    ^(?=.*wan)(?!.*(vlan|bridge)).*$
    
    • ^ - start of string.
    • (?=.*wan) - positive lookahead. condition for wan must be in line.
    • (?!.*(vlan|bridge)) - negative lookahead. condition for vlan and bridge must not be in line.
    • .* - match anything except new line.
    • $ - end of string.

    Demo