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
In reply to Re: Parsing a boolean search string for SQL query
by herveus
in thread Parsing a boolean search string for SQL query
by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |