in reply to Help with search query

Perhaps you want something like (untested):
my @parts = $in{'searchtextterms'} =~ /(?|"([^"]*)"|'([^']*)'|(\S+))/g +;
This doesn't allow for escaping the quotes. It also may not do what you want it to do on input like:
foo'bar
This will break your SQL queries as presented, as you're interpolating and not using place holders. In fact, the interpolating may allow for SQL injection attacks, but your input may be trusted, or you may do your scrubbing elsewhere; I cannot judge that from your post.

Replies are listed 'Best First'.
Re^2: Help with search query
by Anonymous Monk on Mar 27, 2012 at 09:36 UTC

    Search::QueryParser supports quoted constructs :) But if you want your regex see Regexp::Grammars::Common::String or Regexp::Common

    use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( qr/(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")|(?:\')(?:[^\\\']*(?:\\. +[^\\\']*)*)(?:\')|(?:\`)(?:[^\\\`]*(?:\\.[^\\\`]*)*)(?:\`))/ )->explain; __END__ The regular expression: (?-imsx:(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")|(?:\')(?:[^\\\']*( +?:\\.[^\\\']*)*)(?:\')|(?:\`)(?:[^\\\`]*(?:\\.[^\\\`]*)*)(?:\`))) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \" '"' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- [^\\\"]* any character except: '\\', '\"' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- (?: group, but do not capture (0 or more times (matching the most amount possible)): ---------------------------------------------------------------------- \\ '\' ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- [^\\\"]* any character except: '\\', '\"' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- )* end of grouping ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \" '"' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \' ''' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- [^\\\']* any character except: '\\', '\'' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- (?: group, but do not capture (0 or more times (matching the most amount possible)): ---------------------------------------------------------------------- \\ '\' ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- [^\\\']* any character except: '\\', '\'' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- )* end of grouping ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \' ''' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \` '`' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- [^\\\`]* any character except: '\\', '\`' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- (?: group, but do not capture (0 or more times (matching the most amount possible)): ---------------------------------------------------------------------- \\ '\' ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- [^\\\`]* any character except: '\\', '\`' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- )* end of grouping ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \` '`' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------