Q: I was using Value Search in Studio 3T, searching for “
test” and I noticed the Regular Expression checkbox on the search window. Checking it made no difference to the results of my searches so what does it actually do?
A: The reason you aren’t seeing any difference in your search results is because your simple string matches the same way whether or not you have regular expressions on or off.
Where regular expressions come into their own is where you want to be more or less precise through the use of “special” characters.
If you think of wildcards, like the
* in filenames, where a character takes on a special meaning (in this case to match any part of a filename), then you can think of Regular Expressions as that, but on steroids. And by that we mean it’s a super powerful way of matching strings, not just filenames, and they are supported in many many tools.
There are entire books about regular expressions, so we’ll just introduce some super useful characters you can use.
Anchoring your regular expression match
If you are looking for the word “test” only when it appears at the start of a field, prefix text with the
^ character to anchor your match to the start of any field like so:
If you want your match to only happen at the end of a field, append a
$ to anchor the match with the end, like this:
Regular expressions for anything
Regular expressions have their own version of a wildcard. Specifically the dot/full stop character, which says “match any character”. So if you wanted to match text, tent, and test you would search for:
You do see regular expressions with the * character in them, but this isn’t a wildcard as used in file-name matching. It says “match with zero or more of the preceding character”, so if you used:
then you might see something like this:
What’s happened here? Well, the
* says zero or more, and while this match would match with:
It will also match with
tt which we find in a URL’s https:// opening as there is zero
e between the two
t. This can catch people out with some unexpected matches. If you want to be sure there’s one or more occurrences of your letter, use +.
te+t would match everything but
Another thing to remember, if what you are searching for with a regular expression contains one of the special characters (and there are more of them) you’ll need to escape them by preceding each with a backslash. For example, searching for
\.$ will find values which end with a full stop.
Bringing them together
Finally, on this brief tour of regular expressions for value searchers, you can bring all these special characters together. You can use the
* to say one/none or more of any character. And you can anchor that match to the start or end of a field’s values.
Here, we are matching any field that begins with a letter
t, with one or more of any character, up to another
t. Regular expression matchers are usually greedy in that they try to match as much as possible. Here you can see they are matching all the way up to the last t in field values.
Regular expressions let you finely filter your matching results when you are digging through your collections. In combination with Value Search’s collection, database, and server scanning, they can really power up your productivity. If you are wondering about regular expressions elsewhere in Studio 3T, check out this AMA on Regular Expressions, MongoDB and the Visual Query Builder.
Exploring regular expressions with Value Search is easy and quick. If you want to experiment with a more helpful interface, check out regex101.com where your regular expressions will automatically be explained.