RE: [PHP] Regex help

From: Date: Sat, 29 Jan 2005 18:48:40 +0000
Subject: RE: [PHP] Regex help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-207683@lists.php.net to get a copy of this message
On Sat, 2005-01-29 at 08:58, Michael Sims wrote: > kjohnson@zootweb.com wrote: > > OK, this is off-topic like every other "regex help" post, but I know > > some of you enjoy these puzzles :) > > This isn't an exam question, is it? ;) > > > I need a validation regex that will "pass" a string. The string can > > be no longer than some maximum length, and it can contain any > > characters except two consecutive ampersands (&) anywhere in the > > string. > > > > I'm stumped - ideas? > > Yup, use this perl regex: > > /^(?:(&)(?!&)|[^&]){1,5}$/ > > Where 5 above is the maximum length of the string. You can change this to > any positive value and the regex will still work. Basically it says "look > for 1 to 5 single characters where each either isn't an ampersand, or IS an > ampersand but isn't immediately followed by an ampersand". The (?!&) is a > zero-width negative look-ahead assertion which is like other assertions such > as "\b" that don't eat up the portions of the string that they match. Great explanation. Thanks from one who has not had an exam for over ten years. Bret

« previous php.general (#207683) next »