in reply to regex die?

I think part of your problem is that you are using square brackets [] inappropriately. They set up a character class so your [.]+ will match one or more literal full-stops; in a character class the full-stop loses it's meta-character meaning of matching any character and becomes a literal. Doing [abc]{3} would match exactly three of either a or b or c.

It also makes things easier when matching paths to choose a delimiter character other than "/" by using the m operator. You can choose a character like the pipe symbol "|" or balanced brackets like {}. Once you've done that you no longer have to escape the "/" characters.

My (non-tested) attempt at your sub die_clean would be:-

sub die_clean { my $input = shift; my ($msg, $line) = $input =~ m{(.+?)( at /\S+ line \d+\.$}; }

The $ sign anchors the match to the end of the string just on the off-chance that the "MY ERROR" part contains "at /pathe/to/file line n." which is unlikely but stranger things have happened. The .+? says match one or more of any character in a non-greedy fashion. You want to do this otherwise a .+ without the ? would consume the entire string and not leave any characters for the rest of the regular expression to match.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: regex die?
by ayrnieu (Beadle) on Mar 19, 2006 at 22:31 UTC

    There's no need to for /\S+, however. Better to simply .+?

      Six of one, a half dozen of the other unless /path/to/file contains spaces. It easily could, of course, but I mainly work on Unix-like systems so it is a bit of a blind spot with me :-)

      Cheers

      JohnGG

        OP did include a space in his match set and also \, both of which are a bit of a hint. :)

        I was under the impression the *nix could handle spaces in directories/file names in any case - it's just that because no one much uses them, *nix applications are even worse at being robust against spaces than Windows applications are.

        /me puts his stiring stick back in the corner. :)


        DWIM is Perl's answer to Gödel
        Alas, paths come with spaces even on unix.