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

quoting meta chars in regex containing variables

Thread Next
Peter Daum
August 29, 2009 08:44
quoting meta chars in regex containing variables
Message ID:
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?


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About