Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to not match "www"

Tags:

regex

I guess I'm getting really weak in logic.

I need to write a regular expression which matches everything except www. It should match wwwd, abcd and everything else, just not www. (Oh God, please, it shouldn't be very easy).

I'm using Ruby language's implementation of regular expression.

UPDATE: I need to use regular expression and not just text != 'www' because it is the way API is designed. It expects a pattern as argument and not the result.

like image 892
Vikrant Chaudhary Avatar asked Feb 17 '10 11:02

Vikrant Chaudhary


2 Answers

Why regex? Isn't text != "www" enough?

Here it is nonetheless (uses look-ahead): ^(?!www$).*

like image 179
soulmerge Avatar answered Oct 08 '22 07:10

soulmerge


This is a plain vanilla regex. There are fancier things you can do with negative assertions in certain dialects.

^(.|..|[^w]..|.[^w].|..[^w]|.....*)$

In English:

You want something that's exactly one character, exactly two characters, exactly three characters where at least one of those 3 is not a w, or more than 3 characters long.

like image 35
Laurence Gonsalves Avatar answered Oct 08 '22 06:10

Laurence Gonsalves