brokenman wrote:In any case with a REGEX you could use negative look ahead.
In trying to understand suggested RE, trawled the Net for 'look ahead'. Came across cookkbook recipe for excluding strings containing a certain substring, in this case 'invalid':
Began to look like the suggested RE for 'mchat' would not work. Anyway came across bewildering amount of technical detail and discussion about anchors and efficiency. Forced to go back to first principles and simple logic of what was required.
So, as memo to myself and anyone else:
Wisdom of the Web wrote:Negative look ahead is used to match something not followed by something else.
i.e. if something is not followed by something else we have a match.
^[^u] => Start of String not followed by 'u'
Similarly:
^(?!u) => Start of String not followed by 'u'
All matches being at position 0.
Extrapolating:
^(?!.*mchat) => Start of String not followed by generalized string '.*mchat.*'
Code: Select all
No match: http://archive.linuxfromscratch.org/lfs-museumchat/2.3.1/LFS-BOOK-2.3.1-HTML/index.html
Match: http://media7.fast-torrent.ru/media/js/jquery-ui-1.10.3.custom1.min.js
Match being at position 0.
Expression
(?!^mchat$)(^.*$):
Code: Select all
Match: http://archive.linuxfromscratch.org/lfs-museumchat/2.3.1/LFS-BOOK-2.3.1-HTML/index.html
Match: http://media7.fast-torrent.ru/media/js/jquery-ui-1.10.3.custom1.min.js
Matches being at position 0.
All tests done at
http://www.regular-expressions.info/jav ... ample.html.
So,
^(?!.*mchat) is not very far from cookbook example
^(?!.*invalid.*).*, but someonne commenting there suggested
^(?!invalid)(.(?!invalid))*$, although more complex, would lead to a more simple (less matches) result. This defeats me.
IMHO
^(?!.*mchat) would just be one scan for 'mchat', which would decide the match.
Nonetheless it apppears that the Regular Expression to exclude a string containing a substring
s would be:
^(?!.*s)
Real Life
whitelist example:
'.*porteus\.org(?!.*mchat)'Code: Select all
No match: http://forum.porteus.org/mchat/jquery_cookie_mini.jsg/styles/prosilver/template/forum_fn.js
Match: http://forum.porteus.org/styles/prosilver/template/forum_fn.js
Match: http://forum.porteus.org/chat/jquery_cookie_mini.jshttp://forum.porteus.org/styles/prosilver/template/forum_fn.js
Matches being at position 0 in both cases.
As opposed to
'porteus\.org(?!.*mchat)'Code: Select all
No match: http://forum.porteus.org/mchat/jquery_cookie_mini.js
Match: http://forum.porteus.org/styles/prosilver/template/forum_fn.js
Match: http://forum.porteus.org/chat/jquery_cookie_mini.jshttp://forum.porteus.org/styles/prosilver/template/forum_fn.js
In both cases match at position 13:
porteus.org
Epilog
Code: Select all
SilentBlock: /\.js$/i
blocked http://forum.porteus.org/mchat/jquery-1.5.0.min.js
SilentBlock: /\.js$/i
blocked http://forum.porteus.org/styles/prosilver/template/forum_fn.js
but unblocked by /porteus\.org(?!.*mchat)/i