Reputation: 2979
Given the following document
{
"_id": "test-id",
"text": "this is some text and (0aef4666-3627-4c24-8e50-b0cf9a723823)"
}
The following mongo query returns the document:
{"text":{"$regex":"\\(0aef4666-3627-4c24-8e50-b0cf9a723823\\)","$options":"iu"}}
But this one does not:
{"text":{"$regex":"\\b\\(0aef4666-3627-4c24-8e50-b0cf9a723823\\)\\b","$options":"iu"}}
I want to search for the uid on the word boundary. I thought adding \b would properly pick it up, but it apparently does not match, even if the term I'm looking for is in the middle of the text (I thought perhaps the fact it was at the end of the string was affecting it, but it doesn't appear to be).
Why doesn't this work and how can I get my parenthetical term to match on the "whole word"?
Upvotes: 2
Views: 5105
Reputation: 59681
It's because \b
only matches if there's a word character on either side of it. In your case, the \b
is surround by a space character and a open/close parenthesis, neither of which are a "word" character. Therefore the \b
match fails.
You could do
\\b0aef4666-3627-4c24-8e50-b0cf9a723823\\b
which will match the \b
because now it is on a word boundary.
Alternatively, you could match against a space OR start/end-of-line:
db.test.find({"text": /(^|\s)\(0aef4666-3627-4c24-8e50-b0cf9a723823\)($|\s)/i} )
Upvotes: 2