in reply to Parsing a boolean search string for SQL query

Howdy!

The reason you miss the last term is because the regexp requires the OR/AND/NOT. Put a ? after that group and it picks up the last term (at the expense of an "use of uninitialized value in concatenation" warning).

Alternately, you could split on whitespace and modify the words that aren't OR/AND/NOT...see my tested code below...

#!/usr/bin/perl -w use strict; my $searchstring = "aaaa OR bbbb OR cccc OR dddd"; my @words = split(/\s+/, $searchstring); $searchstring =~ s/(\w+) ?(OR|NOT|AND)?/\(fieldname \= '$1'\) $2/g; foreach (@words) { next if /^(OR|AND|NOT)$/; $_ = "(fieldname = '$_')"; } my $searchstring2 = join(' ', @words); print "regex: $searchstring\nsplit/join:$searchstring2\n";

yours,
Michael

Replies are listed 'Best First'.
2Re: Parsing a boolean search string for SQL query
by jeffa (Bishop) on Sep 01, 2003 at 17:16 UTC
    This is not the first time where i have seen the split solution be more robust than the substitute solution. I just ran your code with the following string:
    my $searchstring = "aaaa OR bbbb OR NOT cccc AND dddd";
    
    And was happy to see the following results (formatted for the Monastery):
    regex:
       (fieldname = 'aaaa') OR 
       (fieldname = 'bbbb') OR 
       (fieldname = 'NOT')
       (fieldname = 'cccc') AND
       (fieldname = 'dddd')
    
    split/join:
       (fieldname = 'aaaa') OR 
       (fieldname = 'bbbb') OR NOT 
       (fieldname = 'cccc') AND 
       (fieldname = 'dddd')
    

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)