[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Regular Expression...



Some time around 8/1/97 1:15 PM , Matt Sansbury wrote something about

>I apologize if this question is a newbie one, but I can't seem to find
>the answer...
>
>I have created a search engine and am trying to search for multiple
>words that are next to each other in a string. For example, I would
>like a search for "red blue" to match the words red and blue seperated
>by one or more spaces, but not "red green blue." Right now I can only
>get the latter to happen.
>
>Any suggestions on how to write the regular expression?
>
>Thanks,
>
>Matt Sansbury

Though it's not a MacPerl-specific question, here's one way to utilize 
the regexp, already posted by some others, in your script:

#### begin code snippet ####
# $match is initially false
# $string is the string input to the search engine
# $line is of course the line you are checking

$match = "false";

$match = &matchit( $string, $line );
print "$match\n";

sub matchit {
	     my ( $the_string, $the_line ) = @_;
     	my @words = split /\s+/, $the_string;
     	my $first = "";
     	my $second = "";
     	if ( scalar @words > 1 ) {
          		$first = shift @words;
          		while ( @words ) {
               			$second = shift @words;
               			# here's where the regexp comes into play
               			if ( $the_line =~ m!\b$first\s+$second\b! ) {
                    				$first = shift @words; # going to check the next 
word now
               			} else {
                    				return "false";
               			}
          		}
     	} elsif ( $the_line !~ m!\b$words[0]\b! ) {
          		return "false";
     	}
     	return "true"; # if we get to here, it's a match
}#endsub matchit
#### end code snippet ####

You would probably want to substitute out punctuation in both $string and 
$line before calling (or while inside) this function, as it will be 
considered part of whatever word it's next to.
ie., in the above sentence, "or" would be "(or", and "inside" would be 
"inside)".

This code has been tested exactly three times in the last minute or so, 
but I've used a similar method in a search engine before.

Hope this helps,

Dave Beverly
webmaster@thecitizennews.com -- http://www.thecitizennews.com/
Mac Manipulator
"I don't do windows!"

**
MacOS is an operating system; OS/2 is half an operating system; windoze 
is a shell; DOS is a boot partition virus... where do you want to go 
today?


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch