Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regular expression problem ? and * characters

Reply
Thread Tools

regular expression problem ? and * characters

 
 
compboy
Guest
Posts: n/a
 
      05-28-2006
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.

 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      05-28-2006
compboy 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*
>


Quote it:

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

see perldoc perlre

DS
 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      05-28-2006
David Squire wrote:
> compboy 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*
>>

>
> Quote it:
>
> if (/\Q$pattern\E) {...
>


Oops.

if (/\Q$pattern\E/) { ...
 
Reply With Quote
 
compboy
Guest
Posts: n/a
 
      05-28-2006
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 Squire wrote:
> David Squire wrote:
> > compboy 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*
> >>

> >
> > Quote it:
> >
> > if (/\Q$pattern\E) {...
> >

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


 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      05-28-2006
compboy wrote:
> 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
 
Reply With Quote
 
compboy
Guest
Posts: n/a
 
      05-28-2006
woooowww....
yes yes
It really WORKS

you are a hero
thanks a lot

David Squire wrote:
> compboy wrote:
> > 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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-28-2006
compboy <(E-Mail Removed)> wrote:

> this is part of the sricpt
>
> chomp = ($pattern = ARGV[0]);



No it isn't!


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-28-2006
>>>>> "c" == compboy <(E-Mail Removed)> writes:

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

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

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need regular expression for at least 7 characters and at least 1 special chatacter AAaron123 ASP .Net 0 10-03-2008 01:25 PM
Python and Cyrillic characters in regular expression phasma Python 5 09-05-2008 05:43 PM
javascript regular expression methods, problem with escape characters lev Javascript 9 09-17-2006 07:49 AM
Regular expression for stripping tab and new line characters in Render paul.hester@gmail.com ASP .Net 1 06-01-2006 09:09 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments