Reputation: 755
I have the follow pattern
Administrator yes
User yes
Userb yes
userc yes
userd yes
I want a match when there is any user followed by yes (with an arbitrary number of spaces in between them) that is NOT Admin,User,and Userb
I've attempted negative lookaheads and negative lookbehind. For example for the case of Administrator, I've tried this
(?<!Administrator)\s+yes
However, there is still a match for the line with Administrator, I suspect because of the \s+. Much help is appreciated, thank you.
Upvotes: 3
Views: 89
Reputation: 44259
Your assumption is correct. \s+
will just match a single space (for example!) and then this space would not be preceded by Administrator
but by another space. This is not easily solved, because including the \s+
into the negative lookbehind is only allowed in .NET (other regex engines cannot handle variable-length lookbehinds).
However, if you use multiline mode (how to activate it depends on your language/tool), then you can use a negative lookahead instead:
^(?!Administrator|User|Userb)\w+\s+yes
In multiline mode ^
will match at the start of every line.
Update: In fact, after writing this, I did come up with a solution using a lookbehind. Simply make sure, that you execute the lookbehind right after a bunch of word-characters:
\w+(?<!Administrator|User|Userb)\s+yes
Now \s+
has to match all of the spaces, otherwise they would not be preceded by word characters.
Note that both solutions are problematic with non-prefix free users. The first one will not match the user Userbob
while the second one will not match ThatUser
. This could be solved using word boundaries:
^(?!(?:Administrator|User|Userb)\b)\w+\s+yes
\w+(?<!\b(?:Administrator|User|Userb))\s+yes
Upvotes: 2
Reputation: 145
This works:
^(?!Administrator|User).*yes
http://www.rubular.com/r/ijyZT0Eaas
Upvotes: 0