tags:

views:

47

answers:

3

Dear Sir

Regular Expression: Not equal to Account and not begin with Account/

How do I express that in a regular expression?

Thank You.

+3  A: 

If your implementation supports look-around assertions, you can do this with a negated look-ahead assertion:

^(?!Account($|/))

The same can be done with a negated look-behind assertion. Otherwise, if you just can use basic syntax, you will probably do something like this:

^($|[^A]|(A($|[^c]|c($|[^c]|c($|[^o]|o($|[^u]|u($|[^n]|n($|[^t]|t[^/]))))))))

But maybe it suffices if you try to match either equal to Account or begins with Account/ and invert the result of the match.

Gumbo
Is a "look-around" required here?
pst
Shouldn't the `^` be outside of the `(?!…)`?
KennyTM
@pst: I’ve also posted a low-level solution.
Gumbo
@KennyTM: Yes, of course. Thanks!
Gumbo
A: 

Imagine the regex (for some regex flavors) /^(Account$|Account\/)/ which only matches "Account" or things that begin with "Account/". Note the use of anchors (^ and $) as appropriate.

Why is the above regex the opposite of the question? In many cases of "not looking for" the outer-regex conditional can (and should) simply be a negation.

pst
A: 

This says 'If the line doesn't belong with Account or contain Account (I know redundant) then match the entire line:

(?! ^Account)(?! ^.Account.)^.*$

Mike Cheel