Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!bu.edu!cs.bu.edu!ckd From: ckd@cs.bu.edu (Christopher Davis) Newsgroups: comp.lang.perl Subject: Re: pattern1 AND pattern2? Message-ID: Date: 25 Apr 91 21:23:14 GMT References: Sender: news@bu.edu.bu.edu Organization: 1000000011000101, Inc. Lines: 38 In-reply-to: dennis@nosc.mil's message of 25 Apr 91 19:03:13 GMT Dennis> == Dennis Cottel Dennis> I know how to get a successful match on either of two patterns: Dennis> "/pattern1|pattern2/". How can I say that both have to match? Dennis> I've read most of the book and skimmed the rest, but I can't Dennis> see it. I can't believe it isn't possible with Perl, though! You didn't say whether they had to match in a particular order, or whether they could overlap, or anything like that... If they have to be pattern1 then pattern2, without overlap, try /pattern1.*pattern2/ If you don't care about order or overlapping, use /pattern1/ && /pattern2/ If you care about order, but they can overlap, hand-hack a regexp to match the potential overlap order. If you had 'foobar' and 'bartender', in that order, you could use /foo(bar)+tender/ || /foobar.*bartender/ If you don't care about order, but they can't overlap, try matching the second one against $` and $'. /pattern1/ && ($` =~ /pattern2/ || $' =~ /pattern2/) Now, I know Larry, Randal, and Tom will come back with better ways to do all this, but I figured I should at least *answer* a question for once... -- [ Christopher Davis - - <..!bu.edu!cs.bu.edu!ckd> ] A message destined for delivery in *your* domain is fair game for anything you may want to do, up to and including translating the entire message, header and all, into Swahili. -- chip@tct.uucp (Chip Salzenberg)