# Regex query with ~ not working as expected

**URL:** <https://discuss.elastic.co/t/regex-query-with-not-working-as-expected/160273>\
**Category:** Elasticsearch\
**Created:** [December 11, 2018, 2:09am UTC](https://discuss.elastic.co/t/regex-query-with-not-working-as-expected/160273 "2018-12-11T02:09:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sailorfina](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@sailorfina](https://discuss.elastic.co/u/sailorfina)\
**Post date:** [December 11, 2018, 2:09am UTC](https://discuss.elastic.co/t/regex-query-with-not-working-as-expected/160273/1 "2018-12-11T02:09:36Z")

</div>

Hi,

I have a keyword field, and the value is email address like:  
"[21fd4444d3b0$28c042c9$ca123123$@abc.com](mailto:21fd4444d3b0%2428c042c9%24ca123123%24@abc.com)"

when I do prefix query, I can get the result:

```
{
   "size": 10, 
   "query": {
     "prefix": {
       "myfield": {
        "value": "<21fd4444d3b0"
       }
     }
   }
 }

```

But when I do aggregation query, I can't get expected result:

```
{
  "size": 10, 
  "query": {
    "regexp": {
      "myfield": {
        "value": "\\<[0-9a-f]{4}**~(0[12])**[0-9a-f]{8}$[0-9af]{8}$[0-9a-f]{8}$@",
        "flags": "ALL"
      }
    }
  },
  "aggs": {
    "NAME": {
      "terms": {
        "field": "myfield",
        "size": 1000
      }
    }
  }
}

```

What I want to match is string that start with "\<" and 4 hex chars, not followed by "01" or 02", and end with \<8 hex\>\<8 hex\>\<8 hex\>$, but I see that the some unexpected value returned, like:

`"<000001d38fe4$20990770$61fb1231$@abc.com>"`

The sixth and seventh chars are "01", this is not expected, because I use "~(0[12])" in the pattern, it should not matched.

Can anyone help to point out what is wrong with my query?

Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 8, 2019, 2:09am UTC](https://discuss.elastic.co/t/regex-query-with-not-working-as-expected/160273/2 "2019-01-08T02:09:43Z")

</div>

Closed on January 8, 2019, 2:09am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
