OT: simple regex question
Matt Davey
mcdavey at mrao.cam.ac.uk
Mon Apr 23 12:19:00 UTC 2007
On Mon, 2007-04-23 at 14:05 +0200, Dario Lesca wrote:
> Il giorno lun, 23/04/2007 alle 12.36 +0100, Matt Davey ha scritto:
>
> > echo "aa bb cc dd" | perl -pe 'exit(!(/(?=.*\bbb\b)(?=.*\bdd\b)/));'
> > && echo found
>
> Ok, Work!
>
> last question ... (and many thanks if you have a little time to spend
> for me)
>
> if I want convert this format into if/else statement.... like this:
>
> $str="aa bb cc dd"
> if ($str =~ /.*(\bb|dd)\b/i)
> {
> print "***\nYes, Match!\n***\n";
> }
> else
> {
> print "Non match!\n";
> }
>
> It's possible? .. (I am not a perl guru, then I do not how to convert
> the previous format in this newer) ....
It's pretty much as you write it above, just a bit more concise to fit
on the command line:
echo "aa bb cc dd" | perl -ne 'if (/(?=.*\bbb\b)(?=.*\bdd\b)/) {print
"found\n"} else {print "not found\n"};'
Or (TMTOWTDI) , using the ? tertiary operator:
echo "aa bb cc dd" | perl -ne 'print (/(?=.*\bbb\b)(?=.*\bdd\b)/ ?
"found\n" : "not found\n");'
Or you can leave the perl bit alone and use the shell:
echo "aa bb cc dd" |
(perl -pe 'exit(!(/(?=.*\bbb\b)(?=.*\bdd\b)/));' && echo found ) || echo not found
Matt
Matt Davey What has four legs and one arm?
mcdavey at mrao.cam.ac.uk A happy Rottweiler.
More information about the fedora-list
mailing list