# Regex string from field value

**URL:** https://discuss.elastic.co/t/regex-string-from-field-value/218298
**Category:** Elasticsearch
**Created:** [February 7, 2020, 8:55am UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298 "2020-02-07T08:55:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ganchu](https://avatars.discourse-cdn.com/v4/letter/g/ecb155/32.png) [@ganchu](https://discuss.elastic.co/u/ganchu)
#### Post date: [February 7, 2020, 8:55am UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298/1 "2020-02-07T08:55:36Z")

</div>

First query can find events included this string  
_`"registry_key_path" : """HKCR\mscfile\shell\open\command\(Default)"""`_

```
GET _search
{
  "query": {
"query_string": {
  "query": "(registry_key_path:/.*mscfile.*/)"
}
  }
}

```

If I change query to below don't find anything.  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/0/20a9402d115fef55f19f9d0e9f4c11f1fe395ecd.png)

---

<div class="post-metadata">

### Author: ![Vadims\_Daleckis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vadims_daleckis/32/57613_2.png) [@Vadims\_Daleckis](https://discuss.elastic.co/u/Vadims_Daleckis)
#### Post date: [February 7, 2020, 1:34pm UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298/2 "2020-02-07T13:34:45Z")

</div>

I can confirm same happens for me. But this question seems to be specific to Elasticsearch, so I changed channel of this question from "Kibana" to "Elasticsearch".

---

<div class="post-metadata">

### Author: ![Vadims\_Daleckis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vadims_daleckis/32/57613_2.png) [@Vadims\_Daleckis](https://discuss.elastic.co/u/Vadims_Daleckis)
#### Post date: [February 7, 2020, 1:47pm UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298/3 "2020-02-07T13:47:43Z")

</div>

If you run:

```
POST _analyze
{
  "text" : "HKCR\\mscfile\\shell\\open\\command\\(Default)"
}

```

you will see how Elasticsearch tokenizes the string:

```
{
  "tokens" : [
    {
      "token" : "hkcr",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "mscfile",
      "start_offset" : 5,
      "end_offset" : 12,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "shell",
      "start_offset" : 13,
      "end_offset" : 18,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "open",
      "start_offset" : 19,
      "end_offset" : 23,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "command",
      "start_offset" : 24,
      "end_offset" : 31,
      "type" : "<ALPHANUM>",
      "position" : 4
    },
    {
      "token" : "default",
      "start_offset" : 33,
      "end_offset" : 40,
      "type" : "<ALPHANUM>",
      "position" : 5
    }
  ]
}

```

My guess is it has to do with how regular expressions work with those tokens, if you `AND` two separate queries like that it fetches your document:

```
GET _search
{
  "query": {
"query_string": {
  "query": "(registry_key_path:/.*mscfile.*/) AND (registry_key_path:/.*shell.*/)"
}
  }
}

```

I realize it does solve your questions, but maybe it helps, and maybe somebody from Elasticsearch can chime in here.

---

<div class="post-metadata">

### Author: ![Vadims\_Daleckis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vadims_daleckis/32/57613_2.png) [@Vadims\_Daleckis](https://discuss.elastic.co/u/Vadims_Daleckis)
#### Post date: [February 7, 2020, 1:51pm UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298/4 "2020-02-07T13:51:44Z")

</div>

Setting analyzer to `keyword` allowed me to successfully execute your query:

```
PUT /test
{
  "settings": {
    "analysis" : {
      "analyzer" : {
        "default" : {
          "type" : "keyword"
        }
      }
    }
  }
}

POST /test/_doc/
{
    "user" : "kimchy",
    "registry_key_path" : "HKCR\\mscfile\\shell\\open\\command\\(Default)"
}

GET /test/_search
{
  "query": {
    "query_string": {
      "query": "(registry_key_path:/.*mscfile.*shell.*/)"
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [February 7, 2020, 1:53pm UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298/5 "2020-02-07T13:53:54Z")

</div>

[Duplicate post](https://discuss.elastic.co/t/regex-with-query-from-string/218312/2)

See my comments re performance

---

<div class="post-metadata">

### Author: ![Vadims\_Daleckis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vadims_daleckis/32/57613_2.png) [@Vadims\_Daleckis](https://discuss.elastic.co/u/Vadims_Daleckis)
#### Post date: [February 7, 2020, 1:54pm UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298/6 "2020-02-07T13:54:01Z")

</div>

```
POST _analyze
{
  "text" : "HKCR\\mscfile\\shell\\open\\command\\(Default)",
  "analyzer" : "keyword"
}

```

result:

```
{
  "tokens" : [
    {
      "token" : """HKCR\mscfile\shell\open\command\(Default)""",
      "start_offset" : 0,
      "end_offset" : 41,
      "type" : "word",
      "position" : 0
    }
  ]
}
```

---

<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: [March 6, 2020, 1:54pm UTC](https://discuss.elastic.co/t/regex-string-from-field-value/218298/7 "2020-03-06T13:54:11Z")

</div>

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