|
hi josh --
In a message dated 7/19/2006 4:22:20 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> Gurus-
> > I'm trying to help a co-worker on reg exp/pattern match issue. > > we have a set of items to go through and another set of input that's > tested against the first. > > we need to check that items in the second set are a one-for-one match or > subset of the latter. > > > ie: > > $var1 =~ /$var2/ > > where this returns true in a case such as $var1=Jose, Joseph, and Josh and > $var2 will be James and Joseph > > when $var2 is James they need to return false and when it is Joseph both > Jose and Joseph should return true while Josh returns false. > > So far we haven't found a solution in orielly and trying > > if( $var1 =~ m/$var2/) { print "$var2 contains $var1\n"; } > else { print "failure on $var2 and $var1\n"; } > > > has failed to work. > > -Josh i may not understand this correctly, it seems to me that you are saying
that items
in the first set (represented by $var1) will always be equal to or less
than the length
of items in the second set (represented by $var2). i'm also
assuming that by
``subset'' you mean ``subset anchored at the beginning of the
string''.
if that is so, it seems that the way to go about comparing the two sets is
the opposite
of the example you give, e.g.,
$var2 =~ /$var1/;
since we don't really care if part of $var2 slops over on the
end.
maybe something like this:
C:[EMAIL PROTECTED]>perl -we "use strict; my $v1 =
shift; for my $v2 (@ARGV) {
my $re = qr/$v2/x;
print qq($v1 ), ($v1 =~ / ^ $re /x) ? q(contains) : q(does NOT contain), qq( $v2 \n) }" Joseph Jose Joseph Josh Josephhh Joseph contains Jose Joseph contains Joseph Joseph does NOT contain Josh Joseph does NOT contain Josephhh note the ^ beginning of string anchor; easy enough to take this out and
have the match
be anywhere in the string.
there's also a version that doesn't involve regexes:
C:[EMAIL PROTECTED]>perl -we "use strict; my $v1 =
shift; for my $v2 (@ARGV) {
print qq($v1 ), (index($v1, $v2) >= 0) ? q(contains) : q(does NOT contain), qq( $v2 \n) }" Joseph Jose Joseph Josh Josephhh Joseph contains Jose Joseph contains Joseph Joseph does NOT contain Josh Joseph does NOT contain Josephhh hth -- bill walters
|
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
