使用字母模式过滤数字



我一直在开发一个应用程序,该应用程序的目标是搜索具有特定数字模式的电话号码。

搜索方案位于下方。

假设搜索模式为:

*ABCABC

那么结果应该是

91203156156
91203487487

如果搜索模式为:

*AABB

那么结果应该是:

91203851122
91203727733

我的问题是

  1. 有没有办法使用带有MongoDB的正则表达式来实现这一点,或者弹性搜索?

  2. 实现这一目标的最佳实践是什么?谢谢。

如果我理解正确,您可以通过分组来实现这一点。例如,如果要查找像ABCABC这样的模式,则可以搜索(.)(.)(.)123,细分如下:

(.)(.)(.)123
(.)                Find any character and put it in the first group
   (.)             Find any character and put it in the second group
      (.)          Find any character and put it in the third group
         1        Match the first group, ie the first character
           2      Match the second group
             3    Match the third group

例如,在 Python 中:

>>> import re
>>> regex = re.compile(r".*(.)(.)(.)123.*")
>>> regex.match("9120**3487487**")
<_sre.SRE_Match object; span=(0, 15), match='9120**3487487**'>

最新更新