[Nottingham] Perl Regular Expression Query.

Tom Bird nottingham at mailman.lug.org.uk
Fri Jan 17 19:06:00 2003


On Fri, 17 Jan 2003, Robert Postill wrote:

> Hi,
> I'm writing some Perl and want to match a string with 4 @ signs separated by a variable number of characters.  Currently I'm trying:
> /@.*?@.*?@.*?@/
> Which is not matching the strings properly.  Has anyone any idea what's wrong?  More importantly has anyone got a way of fixing it? :)
>
> Sample strings to test on are:
> @@@@
> one@two@three@four@
> @a@b@c@d

/^[^@]*(@[^@]*){3}@[^@]*$/

this will ensure that it won't match a string with > 4 @'s in, but allows
anything else leading in and following on from.  '.' in perl regex, will
match an @.

There's probably a neater way still of doing that, but I've been up since
2am and I really can't be arsed to find it.

hth

-- 
Tom  [www.hambule.co.uk] [tom@hambule.co.uk]