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
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
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}$"