regular expression problem ? and * characters

regular expression problem ? and * characters

am 28.05.2006 13:02:48 von compboy

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.

Re: regular expression problem ? and * characters

am 29.05.2006 12:41:27 von rvtol+news

compboy schreef:

fup set to clpm

> Im writing a perl script now and this is part of the sricpt
>
> chomp = ($pattern = ARGV[0]);

That is not real code. ITYM

chomp ($pattern = ARGV[0]) ;


> for each(@thisarray)

That is not real code. ITYM

foreach (@thisarray)

or

for (@thisarray)

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

You can change all that to

/\Q$pattern/ and print "found '$_'\n" ;


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

Read perlre, look for \Q.

(so if you meant wildcards, don't use "\Q").

--
Affijn, Ruud

"Gewoon is een tijger."

Re: regular expression problem ? and * characters

am 29.05.2006 12:41:27 von rvtol+news

compboy schreef:

fup set to clpm

> Im writing a perl script now and this is part of the sricpt
>
> chomp = ($pattern = ARGV[0]);

That is not real code. ITYM

chomp ($pattern = ARGV[0]) ;


> for each(@thisarray)

That is not real code. ITYM

foreach (@thisarray)

or

for (@thisarray)

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

You can change all that to

/\Q$pattern/ and print "found '$_'\n" ;


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

Read perlre, look for \Q.

(so if you meant wildcards, don't use "\Q").

--
Affijn, Ruud

"Gewoon is een tijger."

Re: regular expression problem ? and * characters

am 29.05.2006 14:59:07 von Tad McClellan

Dr.Ruud wrote:
> compboy schreef:
>
>> Im writing a perl script now and this is part of the sricpt
>>
>> chomp = ($pattern = ARGV[0]);
>
> That is not real code. ITYM
>
> chomp ($pattern = ARGV[0]) ;


That still isn't real Perl code. :-)

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


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Re: regular expression problem ? and * characters

am 29.05.2006 20:30:40 von metaperl

compboy wrote:
> Im writing a perl script now and this is part of the sricpt
>
> chomp = ($pattern = ARGV[0]);

you clearly are not using strict
ARGV[0] should be $ARGV[0]

Re: regular expression problem ? and * characters

am 30.05.2006 04:15:55 von Eric Bohlman

"compboy" wrote in news:1148814168.551306.197780@
38g2000cwa.googlegroups.com:

> 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, $_");
> }
> }

No it isn't. That's not Perl. It won't compile.

Please show your actual code, not something that "looks like it."

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

perldoc -f quotemeta