RE: [PHP] Regex help
| From: | Michael Sims | Date: | Sat, 29 Jan 2005 19:13:02 +0000 |
| Subject: | RE: [PHP] Regex help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-207685@lists.php.net to get a copy of this message | ||
Bret Hughes wrote:
> On Sat, 2005-01-29 at 08:58, Michael Sims wrote:
>> kjohnson@zootweb.com wrote:
>>> 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}$/
>
> Great explanation. Thanks from one who has not had an exam for over
> ten years.
Thanks...I actually just realized that the parentheses around the first
ampersand are unnecessary (that was a leftover from a previous attempt), so
this is simpler and will work just as well:
/^(?:&(?!&)|[^&]){1,5}$/
Or if you don't care about capturing portions of the match and then throwing
them away:
/^(&(?!&)|[^&]){1,5}$/