regular expression problem ? and * characters

C

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.
 
D

David Squire

compboy said:
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*

Quote it:

if (/\Q$pattern\E) {...

see perldoc perlre

DS
 
D

David Squire

David said:
compboy said:
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*

Quote it:

if (/\Q$pattern\E) {...

Oops.

if (/\Q$pattern\E/) { ...
 
C

compboy

hii..

It doesnt work correctly

if I put asa?
it will match asas and asas1 (it is not supposed to match asas1, is
it?)

and the if I put a*b*
it is supposed to match aaa and bbb (it will not give this result by
using \Q and \E)

thanks for your answer anyway.

David said:
David said:
compboy said:
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*

Quote it:

if (/\Q$pattern\E) {...

Oops.

if (/\Q$pattern\E/) { ...
 
D

David Squire

compboy said:
hii..

It doesnt work correctly

if I put asa?
it will match asas and asas1 (it is not supposed to match asas1, is
it?)

Yes, it should. 'asas1' certainly includes 'as' followed by 0 or 1 'a'.
I suspect you need to use the start and end of string delimiters ^ and
$. Read the regular expression docs: perldoc perlre.

Example:

----

#!/usr/bin/perl
use strict;
use warnings;

my $pattern = 'asa?';

while (<DATA>) {
print;
print "Pattern: $pattern\n";
if (/$pattern/) {
print "\tMatch\n";
}
else {
print "\tNo match\n";
}
print "Pattern: ^$pattern\$\n";
if (/^$pattern$/) {
print "\tMatch\n";
}
else {
print "\tNo match\n";
}
}


__DATA__
as
asa
asas1
ada
dddas1

----

Output:

as
Pattern: asa?
Match
Pattern: ^asa?$
Match
asa
Pattern: asa?
Match
Pattern: ^asa?$
Match
asas1
Pattern: asa?
Match
Pattern: ^asa?$
No match
ada
Pattern: asa?
No match
Pattern: ^asa?$
No match
dddas1
Pattern: asa?
Match
Pattern: ^asa?$
No match

and the if I put a*b*
it is supposed to match aaa and bbb (it will not give this result by
using \Q and \E)

Ahh. You did not make it clear that you wanted the special characters to
remain special. The \Q and \E delimiters do exactly the opposite - they
allow you to match patterns specified by variables that include special
characters.

DS
 
U

Uri Guttman

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

that code makes no sense. chomp is a function so why are you assigning
to it?

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top