regex - Ruby Regexp trouble. How to accept a string only if it doesnt have 'foo' -
i need regex accept this:
"add chain=input comment="test" protocol=icmp blah blah other stuff"
but not accept this:
"add chain=input comment="test" blah blah disabled=yes
so if @ end of string says disabled=yes, don't match it. i've tried this:
(add action=drop chain=input .*(?!disabled=yes)) add action=drop chain=input [^disabled=yes] add action=drop chain=input.*[^(disabled=yes)]
and couple other variations no avail. doing incorrectly?
edit:
def check( line, key ) return line[ key ] ? true : false end check( "add action=drop chain=input comment="test" disabled=yes", /add action=drop chain=input.*[^(disabled=yes)]/ )
this of code, broken down.
use negative lookahead based regex below.
^(?!.*disabled=yes$)add(?: action=drop)? chain=input.*
(?!.*disabled=yes$)
negative lookahead asserts there isn't string disabled=yes
present @ last. , did make action=drop
string optional because didn't find on input string on regex.
Comments
Post a Comment