# Anchored Regex on array of tags

**URL:** <https://discuss.elastic.co/t/anchored-regex-on-array-of-tags/45741>\
**Category:** Elasticsearch\
**Created:** [March 29, 2016, 11:34pm UTC](https://discuss.elastic.co/t/anchored-regex-on-array-of-tags/45741 "2016-03-29T23:34:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronan\_Foucher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ronan_foucher/32/8757_2.png) [@Ronan\_Foucher](https://discuss.elastic.co/u/Ronan_Foucher)\
**Post date:** [March 29, 2016, 11:34pm UTC](https://discuss.elastic.co/t/anchored-regex-on-array-of-tags/45741/1 "2016-03-29T23:34:20Z")

</div>

Hi,  
So I'm having a hard time getting my anchored regex to work the way I want on my array of tags (I disabled analyzing)

```auto
{
   "doc": {
      "mappings": {
         "info": {
            "properties": {
               "tags": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "query": {
                    "properties": {
                        "regexp": {
                            "properties": {
                                "tags": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        }
                    }
                }
         }
    }
}

```

Querying the tags without anchor works fine and returns what I expect which is all tags matching ._linux._

```auto
GET /doc/info/_validate/query?explain
{
  "query": {
    "regexp": {
      "tags": ".*linux.*"
      }
    }
}

```

But say I have a tag "linux\_x86" and a tag "linux", I need to anchor my regex to return only an exact match on ^linux$

```auto
GET /doc/info/_validate/query?explain
{
  "query": {
    "regexp": {
      "tags": "^linux$"
      }
    }
}

```

Which doesn't work (returns empty) on array of strings, the explain isn't really explicit about what it's doing, I'm guessing it's looking for an exact anchored match across all tags instead of anchored match per tag in the array...

Hopefully someone could explain why and help me finding a workaround

Thanks

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [March 30, 2016, 2:13am UTC](https://discuss.elastic.co/t/anchored-regex-on-array-of-tags/45741/2 "2016-03-30T02:13:46Z")

</div>

I recently saw someone doing this in another thread and the comment from one of the ES team was that we don't apply anchors as Lucene always anchors them.

Take a look at [Regexp support for matching at the beginning (^) and the end ($)](https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030/4) for more

---

<div class="post-metadata">

**Author:** ![Ronan\_Foucher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ronan_foucher/32/8757_2.png) [@Ronan\_Foucher](https://discuss.elastic.co/u/Ronan_Foucher)\
**Post date:** [March 30, 2016, 4:56pm UTC](https://discuss.elastic.co/t/anchored-regex-on-array-of-tags/45741/3 "2016-03-30T16:56:57Z")

</div>

Thanks a lot ! You saved my day 😉

Yeah I was creating a double anchor by adding it in the pattern. Works better without them.

---

<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:** [July 5, 2017, 11:03pm UTC](https://discuss.elastic.co/t/anchored-regex-on-array-of-tags/45741/4 "2017-07-05T23:03:58Z")

</div>


