$foo = qq{^snafu^|^foobar^\n}; $foo =~ m/\A(\W) # \A instead of ^ and match first non-word .+? # +? Minimal match everything that isn't in \1 \1(\W) # Match non-word following the 2nd \1 /xms; $TEXT_QUAL = $1; $FIELD_SEP = $2;