pattern matching in elastic search?

前端 未结 2 1428
醉酒成梦
醉酒成梦 2021-01-23 07:53

Continuing from my earlier post, I have changed the query as according to femtoRgon\'s post some characters and anchors are not supported by elastic search.

I am looking

相关标签:
2条回答
  • 2021-01-23 08:28

    Seeing as ^, $ and \d can't be used, I would do this:

    [^0-9-][0-9]{3}-[0-9]{2}-[0-9]{4}[^0-9-]
    

    Or in Java:

    FilterBuilders.regexpFilter("_all", "[^0-9-][0-9]{3}-[0-9]{2}-[0-9]{4}[^0-9-]"));
    

    Which checks that before or after the found number are no other numbers or dashes. It does require there be some character before and after the match though, so this won't capture documents that have the social security number as the very beginning or very end.

    Regex101 demo

    0 讨论(0)
  • 2021-01-23 08:45

    You forget to add - before ? in your regex and also use anchors if necessary.

    "[0-9]{3}-?[0-9]{2}-?[0-9]{4}"
    

    OR

    "^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$"
    
    0 讨论(0)
提交回复
热议问题