For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > regular expression problem ? and * characters









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 regular expression problem ? and * characters
compboy

2006-05-28, 7:58 am

Im writing a perl script now and this is part of the sricpt

chomp = ($pattern = ARGV[0]);

for each(@thisarray)
{
if($_ =~ m/$pattern/i)
{
print ("found it here, $_");
}
}

the array @thisarray is given.

this scprit reads from the command line and pass that input the the
pattern
and will check if the pattern match the any string inside the array it
will
print the msg.

I have done this part succesfully if the input is just a normal string
like a ab

my question is how do you imporve it so it can accept the input that
contains* and ?
character(s) like *ab? a*b* *a*

thanks a lot.

boyd

2006-05-30, 7:57 am

In article <1148814104.790698.106250@u72g2000cwu.googlegroups.com>,
"compboy" <compboyxyz@gmail.com> wrote:

> Im writing a perl script now and this is part of the sricpt
>
> chomp = ($pattern = ARGV[0]);
>
> for each(@thisarray)
> {
> if($_ =~ m/$pattern/i)
> {
> print ("found it here, $_");
> }
> }
>
> the array @thisarray is given.
>
> this scprit reads from the command line and pass that input the the
> pattern
> and will check if the pattern match the any string inside the array it
> will
> print the msg.
>
> I have done this part succesfully if the input is just a normal string
> like a ab
>
> my question is how do you imporve it so it can accept the input that
> contains* and ?
> character(s) like *ab? a*b* *a*
>
> thanks a lot.


I think you only have to put the '*ab', etc. in single quotes on the
command line.

This example works if you do that: (I called it reg.pl)

#!/usr/bin/perl

use strict;
use warnings;
use Carp;
$|++;

my $regex = shift;

my @array = qw(this that there fore);

for(@array) {
print $_, "\n" if /$regex/;
}
__END__

When I run it with
reg.pl 'th*'
I got:
this
that
there

Boyd
Jer 29.11
Sponsored Links







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

Copyright 2008 codecomments.com