#! perl -slw use strict; ## Convert query syntax to evalable Perl code sub buildQuery { local $_ = uc shift; while( m[ \( (?: AND | OR ) | != | = ]x ) { s< ( [^!=()\s]+ ) ( != | = ) ( [^()\s]+ ) >{ my $op = ( $2 eq '!=' ? 'ne' : 'eq' ); "[\$\L$1\E $op '$3']"; }xge; s< \( \s* ( AND ) \s+ ( \[ [^()]+ \] ) \s+ ( \[ [^()]+ \] ) \s* \) > { "[$2 && $3]" }xge; s< \( \s* ( OR ) \s+ ( \[ [^()]+ \] ) \s+ ( \[ [^()]+ \] ) \s* \) > { "[$2 || $3]" }xge; } tr/[]/()/; return $_; } ## Read data and UPPER case my $data = do{ local $/; uc }; ## Some variables my( $author, $profit, $publisher, $book ); ## And a regex to populate them from each record my $re = qr[ PAGE \s+ \d+ \s+ AUTHOR: \s+ ( [^\n]+ ) (?{ $author = $^N }) \s+ PROFIT: \s+ ( [^\n]+ ) (?{ $profit = $^N }) \s+ PUBLISHER: \s+ ( [^\n]+ ) (?{ $publisher = $^N }) \s+ BOOK: \s+ ( [^\n]+ ) (?{ $book = $^N }) \s+ ]x; ## Covert the query NOTE: (AND ) syntax is required ## where example used implicit AND my $query = buildQuery( <## [ 9:25:03.05]C:\test>670477 Query: (((($author eq 'JOHN') && ($profit eq '90%')) || (($author eq 'MATT') && ($profit eq '80%'))) && ($publisher eq 'OREILLY')) PAGE 2 AUTHOR: JOHN PROFIT: 90% PUBLISHER: OREILLY BOOK: ALGORITHMS PAGE 4 AUTHOR: MATT PROFIT: 80% PUBLISHER: OREILLY BOOK: COMMUNICATION SYSTEMS Query: ((($author eq 'JOHN') && ($profit ne '90%')) || (($author eq 'MATT') && ($publisher ne 'OREILLY'))) PAGE 1 AUTHOR: JOHN PROFIT: 20% PUBLISHER: TMH BOOK: OPERATING SYSTEMS PAGE 3 AUTHOR: MATT PROFIT: 80% PUBLISHER: TMH BOOK: COMPUTER NETWORKS