[Nottingham] Perl Regular Expression Query.

Robert Postill nottingham at mailman.lug.org.uk
Fri Jan 17 14:44:01 2003


Hi,
Looks like this did the trick.  Thank goodness.  I've been at this with =
the question marks because I was told the regex engine would swallow the =
intermediate @ signs.  So thanks for that.

And cheers Michael. I'm impressed how many stabs at the same issue you =
can manage.  But isn't that always the way with Perl? :)

Robert.
P.S. My error was compounded by a logic error that meant it always =
chucked an error whenever the string was OK. DOH! [slaps forehead =
repeatedly]

-----Original Message-----
From: peter.chang@nottingham.ac.uk [mailto:peter.chang@nottingham.ac.uk]
Sent: 17 January 2003 14:12
To: Nottingham LUG (E-mail)
Subject: Re: [Nottingham] Perl Regular Expression Query.



Hi,

On Fri, 17 Jan 2003, Robert Postill wrote:
> 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? :)

I'm not by any imagination a perl programmer but from what I know about
regex, the '?'s are erroneous. You should be using /@.*@.*@.*@/, in fact
using s2p for sed 's/@.*@.*@.*@/crap/' gives

---8<---
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if $running_under_some_shell;

while ($ARGV[0] =3D~ /^-/) {
    $_ =3D shift;
  last if /^--/;
    if (/^-n/) {
        $nflag++;
        next;
    }
    die "I don't recognize this switch: $_\\n";
}
$printit++ unless $nflag;

$\ =3D "\n";              # automatically add newline on print

LINE:
while (<>) {
    chop;
    s/@.*@.*@.*@/crap/;
    print if $printit;
}
---8<---

and saving that as scrap.pl then allows us to test it.

$ echo -e '@@@@\none@two@three@four@\n@a@b@c@d' | perl scrap.pl
crap
onecrap
crapd

hth,
 Peter



_______________________________________________
Nottingham mailing list
Nottingham@mailman.lug.org.uk
http://mailman.lug.org.uk/mailman/listinfo/nottingham