Query Rules: RegEx Match Anywhere in Query

I was asked to have a look at an old unanswered question on Stack Exchange: “Can you create a Query Rule that matches in the middle of the query?”. Challenge accepted. I looked at all of the other options and they either match the beginning, the end, or the entire query. This was SharePoint Enterprise. Good thing. SharePoint Online does not support RegEx in Query Rules, but SharePoint Enterprise does.

Why RegEx?

MVP Andrew Connell loves to ask “If you solve a problem with RegEx, what have you got?” “Two Problems.” I have to agree, I really suck at RegEx. I try not to use it, but when I do I tend to rely very heavily on RegEx testers like RegEx101.com.

The Query Rule Solution

I solved this in SharePoint 2013 Enterprise with a Query Rule using Advanced Query Text Match. If my chosen keyword is Ruby create the following rule:

  1. Query Conditions - Advanced Query Text Match
  2. Query matches this regex:
(.*?)(?i:\bruby\b)(.*?)

if you want the keyword anywhere in any string omit the \b word boundary operator like this

(.\*?)(?i:ruby)(.\*?)

I am NOT a RegEx expert (not even close) so there are probably 100 different ways to write this. My understanding is rule matches the case insensitive phrase “ruby” anywhere in the query with zero or more words before or after the keyword. The thing I learned is that since “Entire query matches exactly” is the default, your RegEx has to successfully match all combinations of the query. I tested this with the following combinations of query terms:

  • ruby
  • Ruby
  • RUBY
  • this is ruby
  • ruby is this
  • I love Ruby so very much
  • ruby charter project
  • charter ruby project
  • charter project ruby
  • ruby video charter project powerful
  • video ruby charter project powerful
  • video charter ruby project powerful
  • video charter project ruby powerful
  • video charter project powerful ruby

It did not hit:

  • charterruby project
  • charter rubyproject
  • charterrubyproject

Extra Credit Query Rule

For extra credit, just in case you wanted the option for multiple keywords, I tested

(.\*?)(?i:\bruby\b|\bwilla\b)(.\*?)
and this matches on either “Ruby” or “Willa”. Continue piping additional terms as needed.

For extra extra credit, just in case you wanted to capture the keyword used, you could use

(.\*?)(?<phrase>(?i:\bruby\b|\bwilla\b))(.\*?)
which SharePoint will recognize as a Capture Group and populate the variable {phrase} with the term (in this case “ruby” or “willa”) for use in a Result Block. The Query Variables UI will change to indicate the presence of the capture group. Capture Group in Query Variable

Here is the RegEx: Sample RegEx

One Final Thought

There are plenty of good reasons to avoid RegEx. You should consider that there is a performance penalty when the query has to decipher regex. (Which way be why it’s not available in SharePoint Online.) Another, as pointed out by Mikael Svenson, is that advanced queries, like those containing a property query, phrase, or operator, may not match. So you really need to decide what problem you are trying to solve and use the appropriate tool!

|| Query Rules || Search || SharePoint 2013 || SharePoint 2016

comments powered by Disqus

Let's Get In Touch!


Ready to start your next project with us? That’s great! Give us a call or send us an email and we will get back to you as soon as possible!

+1.512.539.0322