|
Posted by Bret Hughes on 01/29/05 20:48
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
[Back to original message]
|