develooper Front page | perl.beginners | Postings from August 2009

quoting meta chars in regex containing variables

Thread Next
From:
Peter Daum
Date:
August 29, 2009 08:44
Subject:
quoting meta chars in regex containing variables
Message ID:
h7bicr$vsv$1@ger.gmane.org
I'm struggling with a tricky quoting problem:

I need to split lines divided by some delimiter;

- The delimiter usually will be '|',
   but can be changed, so it needs to be variable.
- Furthermore, the columns may also contain the delimiter,
   in which case it is quoted by a backslash

No problem, as long as I know the delimiter in advance:

my $split_rx= qr[(?<!\\)\|];
my (@cols)= split $split_rx, $line;

But with the delimiter contained in a variable,
no matter what I try, I just can't get an equivalent regex:

my $delimiter='|';

   $split_rx= qr[(?<!\\)\$delimiter];
   # the '$' is quoted, so the variable won't be expanded;

   $split_rx= qr[(?<!\\)\\$delimiter];
   # now the backslash is is quoted instead

I also tried variations using quotemeta or the 'x' modifier,
but I just don't get it right :-(

Any ideas?

                        Regards,
                                            Peter


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About