in reply to Re^2: Creating regex from arrays (oops)
in thread Creating regex from arrays

Good point. Apart from using quotemeta, it would have been better to do:
$tomatch = join '|', map {local $_ = $_; s#\|#\\|#s; $_} @results;
When actually testing this, I found that my initial solution was flawed in another way: the initial pipe needs to be escaped as well! Does any monk have an idea why that is necessary?

Liz

Replies are listed 'Best First'.
Re^4: Creating regex from arrays (oops)
by tye (Sage) on Jul 29, 2003 at 23:19 UTC

    I think you'll slap your forehead for this one. (:

    Does any monk have an idea why [...] the initial pipe needs to be escaped as well

    For the same reason that you realized that you needed to do some escaping in the first place. /|/ matches "either the empty string or the empty string" while /\|/ and /[|]/ match a single vertical bar character.

    So s#|#\\|#g replaces every empty substring of a string with '\|'.

                    - tye
      *slap*