Volodymyr Babii
Volodymyr Babii

Reputation: 48

Elasticsearch regex to find phone number and email

I tried using a query to search for all those which are like phone numbers or/and email within elasticsearch.

Unfortunately, I can't find correct syntax for it.

{
  "query": {
    "regexp": {
      "biography": {
        "value": PHONE_NUMBER_REGEXP || EMAIL_REGEX # <-- problem is here
      }
    }
  }
}

Maybe somebody has a solution. Thank you!

Upvotes: 1

Views: 1653

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522171

To express OR logic within regex, you need to use an alternation, which consists of two sub-patterns separated by a single |. Consider:

^(?:\+65[0-9]{8}|\S+@gmail\.com)$

This would match any Singapore phone number starting with +65 or any Gmail email address.

Following the comment by @Val below, the actual pattern you would use in ELK would not use ^ and $ anchors:

{
  "query": {
    "regexp": {
      "biography": {
        "value": "\+65[0-9]{8}|\S+@gmail\.com"
      }
    }
  }
}

Upvotes: 1

Related Questions