in reply to Break string into array

Use Search::QueryParser
#!/usr/bin/perl -- use strict; use warnings; ## 2009-09-17- 23:40:30 ## D:\dev\misc\search.queryparser.pl use Search::QueryParser; my $qp = Search::QueryParser->new; my $s = q[dogs OR cats OR "flying fish" OR (shrimp AND squid)]; my $query = $qp->parse($s) or die "Error in query : " . $qp->err; use Data::Dumper; print Dumper( $query ),"\n"; __END__ $VAR1 = { '' => [ { 'value' => 'dogs', 'op' => ':', 'field' => '' }, { 'value' => 'cats', 'op' => ':', 'field' => '' }, { 'quote' => '"', 'value' => 'flying fish', 'op' => ':', 'field' => '' }, { 'value' => { '+' => [ { 'value' => 'shrimp', 'op' => ':', 'field' => '' }, { 'value' => 'squid', 'op' => ':', 'field' => '' } ] }, 'op' => '()', 'field' => '' } ] };

Replies are listed 'Best First'.
Re^2: Break string into array
by ikegami (Patriarch) on Sep 18, 2009 at 07:25 UTC

    Good find! It even prevents mixing OR and AND like the OP requested.

    Error in query : [dogs OR cats AND "flying fish" OR (shrimp AND squid) +] : cannot mix AND/OR in requests; use parentheses

    Unfortunately, it does a lot more than the OP requested, but it's just a matter of scanning the output for unsupported constructs