具有括号和单词边界的Mongo $正则表达式

时间:2022-10-07 23:34:34

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:

以下mongo查询返回文档:

{"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).

我想在单词边界上搜索uid。我认为添加\ b会正确地拾取它,但它显然不匹配,即使我正在寻找的术语位于文本的中间(我想也许它是在字符串末尾的事实影响了它,但它似乎不是)。

Why doesn't this work and how can I get my parenthetical term to match on the "whole word"?

为什么这不起作用?如何才能使我的括号术语与“整个单词”相匹配?

1 个解决方案

#1


2  

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.

这是因为\ b只有在它的两边都有一个单词字符才匹配。在您的情况下,\ b由空格字符和打开/关闭括号环绕,两者都不是“单词”字符。因此\ b匹配失败。

You could do

你可以做到

\\b0aef4666-3627-4c24-8e50-b0cf9a723823\\b

which will match the \b because now it is on a word boundary.

它将与\ b匹配,因为它现在位于单词边界上。

Alternatively, you could match against a space OR start/end-of-line:

或者,您可以匹配空格OR开头/结尾:

db.test.find({"text": /(^|\s)\(0aef4666-3627-4c24-8e50-b0cf9a723823\)($|\s)/i} )

#1


2  

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.

这是因为\ b只有在它的两边都有一个单词字符才匹配。在您的情况下,\ b由空格字符和打开/关闭括号环绕,两者都不是“单词”字符。因此\ b匹配失败。

You could do

你可以做到

\\b0aef4666-3627-4c24-8e50-b0cf9a723823\\b

which will match the \b because now it is on a word boundary.

它将与\ b匹配,因为它现在位于单词边界上。

Alternatively, you could match against a space OR start/end-of-line:

或者,您可以匹配空格OR开头/结尾:

db.test.find({"text": /(^|\s)\(0aef4666-3627-4c24-8e50-b0cf9a723823\)($|\s)/i} )