string matching question.

Discussion in 'Perl Misc' started by Mohammed Ishaq, Dec 3, 2003.

  1. I have a been trying to do a string match, and the string has []
    characters. I do not want perl to treat this a regular expression, but
    I cannot get it match. I have the following perl snippet, and the
    output of these two searches is different, but I think that both
    searches should match successfully.

    thanks for any light you can throw.

    mi

    #!/usr/bin/perl -w

    my $e="abcde[12]";
    my $f="XYZ[12] abcde[12] XYZ[12]";

    if ( $f =~ /\b\Q$e\E/ ) { print "match\n"; } else { print "no match\n"
    }
    if ( $f =~ /\b\Q$e\E\b/ ) { print "match\n"; } else { print "no
    match\n" }
    Mohammed Ishaq, Dec 3, 2003
    #1
    1. Advertising

  2. Mohammed Ishaq wrote:
    > I have the following perl snippet, and the output of these two
    > searches is different, but I think that both searches should match
    > successfully.


    <snip>

    > my $e="abcde[12]";
    > my $f="XYZ[12] abcde[12] XYZ[12]";
    >
    > if ( $f =~ /\b\Q$e\E/ ) { print "match\n"; } else { print "no match\n" }
    > if ( $f =~ /\b\Q$e\E\b/ ) { print "match\n"; } else { print "no match\n" }


    From perldoc perlre:
    "A word boundary (\b) is a spot between two characters that has a \w
    on one side of it and a \W on the other side of it (in either order),
    counting the imaginary characters off the beginning and end of the
    string as matching a \W."

    Since ] is not included in the \w character class, the second of your
    regexes does not match.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 3, 2003
    #2
    1. Advertising

  3. (Mohammed Ishaq) writes:
    > I have a been trying to do a string match, and the string has []
    > characters. I do not want perl to treat this a regular expression, but
    > I cannot get it match. I have the following perl snippet, and the
    > output of these two searches is different, but I think that both
    > searches should match successfully.


    If you want to match a literal substring, then you don't want a regex,
    you want index():

    my $e="abcde[12]";
    my $f="XYZ[12] abcde[12] XYZ[12]";
    print "$e is in $f\n" if (index($f, $e));

    Regexes are best (IMO) when you need to match a pattern; use index()
    if you're matching the full string.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Dec 8, 2003
    #3
  4. Eric Schwartz wrote:
    > If you want to match a literal substring, then you don't want a
    > regex, you want index():
    >
    > my $e="abcde[12]";
    > my $f="XYZ[12] abcde[12] XYZ[12]";
    > print "$e is in $f\n" if (index($f, $e));
    >
    > Regexes are best (IMO) when you need to match a pattern; use
    > index() if you're matching the full string.


    If I understand OP's message correctly, he wants a regex that matches
    this:

    'XYZ[12] abcde[12] XYZ[12]'

    but not this, for instance:

    'XYZ[12]abcde[12]XYZ[12]'

    You can't make that distinction with index(), can you?

    I gave him an explanation why \b doesn't "work" together with '[' and
    ']' characters, but it just struck me that I didn't provide an
    alternative solution. I suppose that something like this is what he
    actually wants (if he is still around):

    my $e = 'abcde[12]';
    my $f = 'XYZ[12] abcde[12] XYZ[12]';

    if ( $f =~ /[^\[\]\w]\Q$e\E[^\[\]\w]/ ) {
    print "match\n";
    } else {
    print "no match\n"
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 8, 2003
    #4
  5. Mohammed Ishaq

    Anno Siegel Guest

    Eric Schwartz <> wrote in comp.lang.perl.misc:
    > (Mohammed Ishaq) writes:
    > > I have a been trying to do a string match, and the string has []
    > > characters. I do not want perl to treat this a regular expression, but
    > > I cannot get it match. I have the following perl snippet, and the
    > > output of these two searches is different, but I think that both
    > > searches should match successfully.

    >
    > If you want to match a literal substring, then you don't want a regex,
    > you want index():
    >
    > my $e="abcde[12]";
    > my $f="XYZ[12] abcde[12] XYZ[12]";
    > print "$e is in $f\n" if (index($f, $e));


    print "$e is in $f\n" if 1 + index($f, $e);

    Anno
    Anno Siegel, Dec 9, 2003
    #5
    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. Seth
    Replies:
    1
    Views:
    1,074
    Ray Andraka
    Aug 1, 2003
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,291
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    231
    Marc Bissonnette
    Jan 13, 2004
  4. K3
    Replies:
    0
    Views:
    82
  5. Bobby Chamness
    Replies:
    2
    Views:
    225
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page