Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have the following regx which if contained all on the one line, works as it should, but to make the code a little neater, I thought about tidying it up. However, the following doesn't work. Why? Thanks in advance, Regards, Stacy.
m{ /^(\d+) (\d\d):(\d\d):(\d\d) (\d\d)-(\w+)-(\d{4}) (\d+) (\d+\s+\d+ +) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) (-?\d+\.\d+E[-+]\d+) }x;

Replies are listed 'Best First'.
(Ovid) Re: another regx Q
by Ovid (Cardinal) on May 02, 2001 at 03:34 UTC

    Just looking at the first line of the regex:

    /^(\d+) (\d\d):(\d\d):(\d\d) (\d\d)-(\w+)-(\d{4}) (\d+) (\d+\s+\d+)

    The spaces will be ignored with the /x modifier unless you escape them, or use the \s metacharacters.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: another regx Q
by hdp (Beadle) on May 02, 2001 at 08:05 UTC
    Holy cow! Please, please, please use split for this monstrosity.
    @fields = split / +/, $string
    is much cleaner. Your regex will be hell to maintain if it ever needs to change.

    hdp.

Re: another regx Q
by I0 (Priest) on May 02, 2001 at 03:30 UTC
    What is the pattern that is supposed to match? Do you need to quote your whitespace?
      Opps, sorry, the regx is matching a datafile of the following format (all on one line):
      3639 01:04:55 01-Jan-2001 134 51909 50850 1.431333E-07 4.865000E-07 3.291667E-07 1.723590E-05 2.000000E+00 2.000000E+00 2.000000E+00 -1.028000E-05 3.900000E-07
      Regards, Stacy.
Re: another regx Q
by DrZaius (Monk) on May 02, 2001 at 05:11 UTC
    Have you thought about doing:

    # repeated part my $rp = qr!(-?\d+\.\d+E[-+]\d+)!; my $regex = qr!^(\d+)\s(\d\d):(\d\d):(\d\d)\s (\d\d)-(\w+)-(\d{4})\s(\d+)\s(\d+\s+\d+)\s $rp\s$rp\s$rp\s$rp\s$rp\s$rp\s$rp\s$rp\s$rp!x;

    It takes out some redundancy. You could also use a loop/join instead of repeating $rp a bunch of times.

    Just a thought.

(tye)Re: another regx Q
by tye (Sage) on May 02, 2001 at 19:53 UTC

    I agree that you should use split instead or that you need to quote your whitespace if you don't (turn each " " into "\s+").

    But I don't think anyone noticed that you appear to have changed your delimiters to {} but left the old opening / in which will prevent the above regex from matching anything.

            - tye (but my friends call me "Tye")