For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > working with regexp without escaping range symbol









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author working with regexp without escaping range symbol
Aruna Goke

2007-08-05, 4:01 am

hello all,

How can I work with this kind on expression without escaping the range.

#!/usr/bin/perl

use warnings;
use strict;
use DateTime;

my $ystd = DateTime->today->subtract(days => 1);
my $mdy = $ystd->ymd('-'); #date format in 09-25-2007
my $dty = $ystd->mdy(''); #date in format 09252007
{
my ($sum, $newsum_b, $sum_b, $cnt_ans, $cnt_nans);
open my $fh, '<', $mfile or die "open '$mfile': $!" ;
open my $nfh, '>', $yfile or die "open '$yfile': $!" ;
while (<$fh> ){
if(/$ystd/){
cdr_proc();
}


if I used the date 2007\-08\-04, it will work.. but I want to work with
the regexp without escaping the -.

Thanks for ur help.

Goksie
Jeff Pang

2007-08-05, 4:01 am



-----Original Message-----
>From: Aruna Goke <myklass@gmail.com>
>Sent: Aug 5, 2007 12:39 PM
>To: beginners@perl.org
>Subject: working with regexp without escaping range symbol
>
>hello all,
>
>How can I work with this kind on expression without escaping the range.
>
>#!/usr/bin/perl
>
>use warnings;
>use strict;
>use DateTime;
>
>my $ystd = DateTime->today->subtract(days => 1);
>my $mdy = $ystd->ymd('-'); #date format in 09-25-2007
>my $dty = $ystd->mdy(''); #date in format 09252007
>{
>my ($sum, $newsum_b, $sum_b, $cnt_ans, $cnt_nans);
> open my $fh, '<', $mfile or die "open '$mfile': $!" ;
> open my $nfh, '>', $yfile or die "open '$yfile': $!" ;
> while (<$fh> ){
> if(/$ystd/){
> cdr_proc();
>}
>
>
>if I used the date 2007\-08\-04, it will work.. but I want to work with
>the regexp without escaping the -.
>


You may need quotemeta function.See perldoc -f quotemeta for details.

--
Jeff Pang <pangj@earthlink.net>
http://home.arcor.de/jeffpang/
Paul Lalli

2007-08-06, 6:59 pm

On Aug 5, 12:39 am, mykl...@gmail.com (Aruna Goke) wrote:
> How can I work with this kind on expression without escaping the range.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
> use DateTime;
>
> my $ystd = DateTime->today->subtract(days => 1);
> my $mdy = $ystd->ymd('-'); #date format in 09-25-2007
> my $dty = $ystd->mdy(''); #date in format 09252007
> {
> my ($sum, $newsum_b, $sum_b, $cnt_ans, $cnt_nans);
> open my $fh, '<', $mfile or die "open '$mfile': $!" ;
> open my $nfh, '>', $yfile or die "open '$yfile': $!" ;
> while (<$fh> ){
> if(/$ystd/){
> cdr_proc();
>
> }
>
> if I used the date 2007\-08\-04, it will work.. but I want to work with
> the regexp without escaping the -.


It works just fine without escaping anything. The - is not a special
character in Regular Expressions. It is only a special character in
character classes, which your Regexp doesn't have.

If you actually *did* have characters you needed to escape in your
variable, you'd want the \Q...\E escape sequences, or the quotemeta
function:
perldoc perlre
perldoc -f quotemeta

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com