OT: simple regex question

Matt Davey mcdavey at mrao.cam.ac.uk
Mon Apr 23 11:36:05 UTC 2007


On Mon, 2007-04-23 at 12:44 +0200, Dario Lesca wrote:
> Il giorno lun, 23/04/2007 alle 11.05 +0100, Matt Davey ha scritto:
> > echo "aa bb cc dd" | perl -pe 'exit(!(/(?=.*bb)(?=.*aa)/));'
> 
> Ok for perl, but I must match the word 'aa' and (for example) 'bb',
> someone like this: /(\saa\s)&(\sbb\s)/ .....
> 
> Your example found also "aa xxbb cc dd" and is not what I want...

you can use \b to indicate a word boundary.  So, to exclude xxbb and
bbxx, you can do:

echo "aa bb cc dd" | perl -pe 'exit(!(/(?=.*\bbb\b)(?=.*\bdd\b)/));'
    && echo found

Note: to previous poster who reckoned it matched either word, that may
have been because my first snippet was looking for 'aa' and 'bb', not
'bb' and 'dd' as in the original posting.

Matt Davey		It takes one drink to get me drunk, but I can't 
mcdavey at mrao.cam.ac.uk 	     remember if it's the 13th or 14th -- George Burns




More information about the fedora-list mailing list