in reply to regex trouble

Your description is such a mess due to bad HTML that I'm not sure what you really want to do, but most likely your problem is that \Q quotes meta characters so that the contents of $pattern are treated as a string to match. Omit \Q and in this case your life may be happier.

You also need to quote all the meta characters in $pattern and you need to "double quote" in a double quoted string: "(,|\\||:|>|\\]\\[|_\\|_)" so the quote character is available to be parsed by the regex engine. The two changed lines then become:

my $pattern= "(,|\\||:|>|\\]\\[|_\\|_)"; my @splitted= split /$pattern/,$text;

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: regex trouble
by blazar (Canon) on May 30, 2007 at 11:49 UTC

    The two changed lines then become:

    my $pattern= "(,|\\||:|>|\\]\\[|_\\|_)"; my @splitted= split /$pattern/,$text;

    I know you're trying to stay as close as possible to the OP's code, but as an additional recommendation, for maximum clarity in such a situation one should really follow a cleaner approach like trwww's++ or if using a single hardcoded regex, then taking advantage of the /x modifier and throw in suitable whitespace.

      At some point adding whitespace

      leads

      to

      reduced

      clarity

      and

      slower

      comprehension

      .

      Two things would clean those particular lines up in my view - adding /x as you suggest, and using a character set for the single character delimiters:

      my $pattern= "([,|:>] | \\]\\[ | _\\|_)"; my @splitted= split /$pattern/x, $text;

      DWIM is Perl's answer to Gödel