Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Studio 3T® Knowledge Base

  • Documentation
  • Tutorials
  • Workshops
Take the fastest route to learning MongoDB. Cover the basics in two hours with MongoDB 101, no registration required.
Start the free course

Regular Expressions in Studio 3T

Posted on: 23/06/2022 (last updated: 07/07/2022) by Dj Walker-Morgan

Regular Expressions are a powerful way of expressing string patterns for matching. Value Search uses them to allow you to home in on particular strings in the database with precision. Regular Expressions are commonly available with many languages and applications and the syntax and capabilities of them have a common core.

We recommend regex101.com as an excellent resource to help understand how your regular expressions work – as well as running your regular expressions against text and showing you your matches, it produces an explanation of your expression.

The Simplest Match with Regular Expressions

If the characters in the regular expression exactly match the text being matched with, that’s a match:

Regular expressionTextMatch?
hellohelloYes, hello – We show where the expression matched with italics
hellshellYes, shell – a regular expression matches when the pattern is matched – it doesn’t have to match at the start of the text
hellohellNo – there’s only four characters in the text and the regular expression wants five characters
hellohelloisanyoneinYes, helloisanyonein – A regular expression matches when the pattern is matched and here hello is matched immediately, the rest of the text is irrelevant

Any Single Character – The dot .

A single . represents a single character, any character. 

Regular expressionTextMatch?
.aYes, a
.aaYes, aa – a regular expression matches when the pattern is matched
..aNo – there’s only one character and the regular expression wants two characters
..aaYes, aa – there’s two characters in the pattern and two characters in the text

You can use this . and other characters in combination:

Regular expressionTextMatch?
h.llohullo and helloYes, hullo and hello
h….hodelYes, hodel this is matching h followed by any 4 characters
.hello.helloNo, the expression wants any character before and after ‘hello’
say.hellosay helloYes, say hello – when we say any character, we mean it – a space is a character so the . matches it.

The + and * Multiple Operators

So far, so simple; the patterns are one-to-one in terms of what they match. But regular expressions start being expressions with the + and * operators. A + following a character indicates that the character should appear one or more times for a match. You can also use the . to match any character with the + to match one or more of any character

Regular expressionTextMatch?
a+manateeYes, manatee
e+manateeYes, manatee
d+waddleYes, waddle
d+wonderedYes, wondered
m.+emanateeYes, manatee
m.+emeNo. There’s no characters between the m and the e.
m.+tmanateeYes, manatee

A * says that the character should appear zero or more times. Matching with nothing is somewhat counter-intuitive. You can, for example, have a regular expression of a* and that will match any string at all because there’ll everything matches with no a. That’s why it’s important to use a sequence of characters which terminate your pattern if you use *. 

Regular expressionTextMatch?
a*manateeYes (Twice for each ‘a’ occurring once, and five times for all characters which contain zero a – which is probably not what you want)
a*teemanateeYes – manatee (it doesn’t match the first a as that is followed by an n)
d*lwaddleYes waddle
w.*dwonderedYes wondered (regular expressions seek the biggest matching sequence)
w.*dwdYes, wd
w.*dwouldYes, would
.*emanateeYes, manatee (seeking the biggest matching sequence again).

For completeness, there is also an ? operator which matches none or one of a character. 

The Alternative Operator |

Where there may be two sequences of characters that you want to match against, the alternative operator | (the vertical bar, also known as the pipe character) will allow the regular expression to match the sequences either side of it. Those sequences can also contain any other operators:

Regular expressionTextMatch?e
a|emanateeYes, manatee
an|tmanateeYes manatee – matches an and t, not an and at 
word|birdThe bird is the wordYes – The bird is the word
w..d|b..dThe bird is the wordYes – The bird is the word
w.+d|b.+dThe bird is the wordYes – The bird is the word – The sequence on the left is greedy and will match to the d in word, so the right hand sequence is never evaluated.

Character Classes with [ and ]

To match with one of a number of characters, you can create a character class. This is a set of characters surrounded by square brackets. So [ame] says “any character that is a, m or e” and would match with a or m or e. 

If the first character in the class is ^ then this inverts the meaning and says “any character that is not a, m or e”.  A - within the characters will create a range. So [a-z] will match any lowercase character from a to z and [0-9] would match a single digit. Multiple ranges can be specified too.

As it is, effectively, a character, then you can use + and * with it too.

Regular expressionTextMatch?
[ame]+manateeYes (Three times, manatee)
[^ame]+manateeYes, (Twice, manatee )
[^ ]+This is a $50 manateeYes – This is a $50 manatee – this matches five times, for each sequence of characters which isn’t a space character.
[a-z]+This is a $50 manateeYes – This is a $50 manatee – only the lowercase alphabetic characters match.
[A-Za-z0-9]+This is a $50 manateeYes – This is a $50 manatee – everything matches but the spaces as the $ character.

Anchor operators ^ and $

A regular expression may be required to match only at the start of a string or at the end of a string. For this, the anchor operators can be used. The ^ represents the start of a string and $ the end of a string.  

Regular expressionTextMatch?
^manmanateeYes, manatee
^teemanateeNo, tee is not at the start of the text
tee$manateeYes, manatee the tee is at the end of the text
n.*$manateeYes, manatee the $ can terminate a multi character sequence
^[^t]+manateeYes – manatee – This is anchored at the start of the line, then followed by the character class [^t] where ^ means “not t”. This will match up to but not including the t in manatee.

Escaping with Backslash \

If you want to match with a character that is one of the operator’s characters, you will need to tell the regular expression matcher not to use them as an operator. This is done by preceding the character with a backslash in a process known as escaping.

Regular expressionTextMatch?
\$[^ ]+This is a $50 manateeYes – This is a $50 manatee – The backslash lets the expression match with the $ in the string. 
\.The end of the line.Yes – The end of the line. – the . at the end matches, because the backslash has made it explicitly a matchable full stop.
\[words\]arrayof[words]Yes, arrayof[words] – the backslash here escapes the square brackets stopping them from defining a character class.
\\the \ characterYes, the \ character – here the backslash escapes itself so you can match with a backslash

Special Characters with Backslash \

The backslash also has another function. Other non-special characters after a backslash can also give powerful, time-saving short cuts. Here’s a list of some of the common ones:

Backslash sequenceMeaning
\sAny whitespace character, including tabs and returns
\SAny non-whitespace character
\dAny digit
\DAny non-digit
\wAny word character, equivalent to [A-Za-z0-9]
\WAny non-word character

Do you want to know more about Regular Expressions?

The full and detailed reference to the Java regular expression engine and the regular expression patterns it accepts is in the Java 17 Documentation. This is the regular expression engine used in the Studio 3T Value Search. 

MongoDB itself uses the de facto standard PCRE library for regular expressions.

For a fuller introduction to Regular Expressions, we recommend O’Reilly’s Introducing Regular Expressions. Beyond that, you can consider Mastering Regular Expressions for a deepest dive into the subject. And finally, as we previously mentioned regex101.com is an excellent interactive resource to help understand how your regular expressions work.


How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Dj Walker-Morgan

Dj has been around since Commodore had Pets and Apples grew everywhere. With a background in Unix and development, he's been around the technology business writing code or writing content ever since.

Article navigation

Related articles

  • How do I use MongoDB regex (regular expressions) in Studio 3T? #Studio3T_AMA
  • The Value of a Regular Expression: #Studio3T_AMA
  • What’s New in Studio 3T 2020.10 | Data Masking, Reschema & The Fastest Studio 3T Version Yet
  • How to Claim and/or Assign Studio 3T Licenses
  • What’s New in Studio 3T 2020.4 | Introducing License Manager, IBM DB2 Migration Support, Export Wizard Improvements

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2023 3T Software Labs Ltd. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum

We value your privacy

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
Accept all
Decline all
Manage cookies
✕

Privacy Preference Center

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Google Analytics

Google Ads

Bing Ads

Facebook

LinkedIn

Quora

Hotjar

Reddit

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

HubSpot

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected