Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To match all allowed fields, the following rather fearful looking regex works:</p> <pre> @((?:cat|mouse|dog|puppy)\b|\((?:(?:cat|mouse|dog|puppy)(?:, *|(?=\))))+\)) </pre> <p>It returns these matches, in order: <code>@cat</code>, <code>@(cat)</code>, <code>@(cat, dog)</code>, <code>@cat</code>, <code>@dog</code>, <code>@(cat, dog)</code>, <code>@mouse</code>.</p> <p>The regex breaks down as follows:</p> <pre> @ # the literal character "@" ( # match group 1 (?:cat|mouse|dog|puppy) # one of your valid search terms (not captured) \b # a word boundary | # or... \( # a literal opening paren (?: # non-capturing group (?:cat|mouse|dog|puppy) # one of your valid search terms (not captured) (?: # non-capturing group , * # a comma "," plus any number of spaces | # or... (?=\)) # a position followed by a closing paren ) # end non-capture group )+ # end non-capture group, repeat \) # a literal closing paren ) # end match group one. </pre> <p>Now to identify any <em>invalid</em> search, you would wrap all that in a negative look-ahead:</p> <pre> @(?!(?:cat|mouse|dog|puppy)\b|\((?:(?:cat|mouse|dog|puppy)(?:, *|(?=\))))+\)) --^^ </pre> <p>This would identify any <code>@</code> character after which an invalid search term (or term combination) was attempted. Modifying it so that it also <em>matches</em> the invalid attempt instead of just pointing at it is not that hard anymore.</p> <p>You would have to prepare <code>(?:cat|mouse|dog|puppy)</code> from your field dynamically and plug it into the static rest of the regex. Should not be too hard to do either.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload