string matching question.

M

Mohammed Ishaq

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" }
 
G

Gunnar Hjalmarsson

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

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

Eric Schwartz

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
 
G

Gunnar Hjalmarsson

Eric said:
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"
}
 
A

Anno Siegel

Eric Schwartz said:
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
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top