regular expression problem ? and * characters

Discussion in 'Perl Misc' started by compboy, May 28, 2006.

  1. compboy

    compboy Guest

    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.
     
    compboy, May 28, 2006
    #1
    1. Advertising

  2. compboy

    David Squire Guest

    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
     
    David Squire, May 28, 2006
    #2
    1. Advertising

  3. compboy

    David Squire Guest

    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/) { ...
     
    David Squire, May 28, 2006
    #3
  4. compboy

    compboy Guest

    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/) { ...
     
    compboy, May 28, 2006
    #4
  5. compboy

    David Squire Guest

    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
     
    David Squire, May 28, 2006
    #5
  6. compboy

    compboy Guest

    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
     
    compboy, May 28, 2006
    #6
  7. compboy <> wrote:

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



    No it isn't!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 28, 2006
    #7
  8. compboy

    Uri Guttman Guest

    >>>>> "c" == compboy <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 28, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,305
  2. Replies:
    1
    Views:
    442
    Hans Kesting
    Jun 1, 2006
  3. phasma
    Replies:
    5
    Views:
    863
    Fredrik Lundh
    Sep 5, 2008
  4. AAaron123
    Replies:
    0
    Views:
    599
    AAaron123
    Oct 3, 2008
  5. lev
    Replies:
    9
    Views:
    129
Loading...

Share This Page