elasticsearch,search,Regex,Mongodb,Algorithm,elasticsearch,Search" /> elasticsearch,search,Regex,Mongodb,Algorithm,elasticsearch,Search" />

Regex 用字母模式过滤数字

Regex 用字母模式过滤数字,regex,mongodb,algorithm,elasticsearch,search,Regex,Mongodb,Algorithm,elasticsearch,Search,我一直在开发一个应用程序,该应用程序的目标是搜索具有特定号码模式的电话号码 搜索场景如下 假设搜索模式为: *ABCABC 那么结果应该是 91203156156 91203487487 如果搜索模式为: *ABCABC *AABB 那么结果应该是: 91203851122 91203727733 我的问题是 使用正则表达式和MongoDB或 弹性搜索 实现这一目标的最佳实践是什么? 谢谢 如果我理解正确,您可以通过分组来实现这一点。 例如,如果要查找类似于ABCABC的模式,可以搜索(

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

搜索场景如下

假设搜索模式为:

*ABCABC
那么结果应该是

91203156156
91203487487
如果搜索模式为:

*ABCABC
*AABB

那么结果应该是:

91203851122
91203727733
我的问题是

  • 使用正则表达式和MongoDB或 弹性搜索

  • 实现这一目标的最佳实践是什么? 谢谢


  • 如果我理解正确,您可以通过分组来实现这一点。 例如,如果要查找类似于
    ABCABC
    的模式,可以搜索
    ()\1\2\3
    ,细分如下:

    (.)(.)(.)\1\2\3
    
    (.)                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".*(.)(.)(.)\1\2\3.*")
    >>> regex.match("9120**3487487**")
    <_sre.SRE_Match object; span=(0, 15), match='9120**3487487**'>
    
    >>重新导入
    >>>regex=re.compile(r.*.\1\2\3.*)
    >>>正则表达式匹配(“9120**3487487**”)
    
    在elasticsearch中有很多正则表达式查询、正则表达式查询、wilcard查询、前缀查询、模糊查询。请先阅读文档,然后问一个具体问题,谢谢您的回复,但搜索场景不仅仅限于ABCABC。模式的可能性是无限的。它可以是*ABCDEFG(其中最后7位为连续数字)、AB*AB(前2位和后2位应相同)等等。