# Regexp support for matching at the beginning (^) and the end ($)

**URL:** <https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030>\
**Category:** Elasticsearch\
**Created:** [March 21, 2016, 5:42pm UTC](https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030 "2016-03-21T17:42:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jinwu](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Jinwu](https://discuss.elastic.co/u/Jinwu)\
**Post date:** [March 21, 2016, 5:42pm UTC](https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030/1 "2016-03-21T17:42:00Z")

</div>

I tried to use a regular expression ^10AB[0-9]{8}$ to match string that start with "10AB" and end with 8 digits, but seems Elasticsearch does not support "^" and "$" to match at the beginning and end. If I do not use "^" and "$" in the regular expression, I will get more than I want, for instance, 10AB00000001, 10AB00000001:count, etc. Note, 10AB00000001::count is not what I want.

So I just want to verify that does Elasticsearch support "^" and "$" to match at the beginning and end; if not, is there any other way to achieve the same purpose? 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 22, 2016, 7:59am UTC](https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030/2 "2016-03-22T07:59:40Z")

</div>

You either use simple lucene syntax - [https://lucene.apache.org/core/2\_9\_4/queryparsersyntax.html](https://lucene.apache.org/core/2_9_4/queryparsersyntax.html) - or the larger ES DSL - [https://www.elastic.co/guide/en/elasticsearch/reference/current//query-dsl.html](https://www.elastic.co/guide/en/elasticsearch/reference/current//query-dsl.html)

I just tried to do a quick test but this didn't seem to work;

```auto
PUT test
{
  "number": "10AB00000001"
}

GET test/_search?q=number=10AB???????1

```

Maybe someone has an idea though.

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [March 22, 2016, 10:21am UTC](https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030/3 "2016-03-22T10:21:29Z")

</div>

Hi Jinwu,

According to the docs, Lucene patterns are always anchored so there is no need for anchoring with "^" and "$". For details please see the [docs on regexp query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html), the [docs on the regex query syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax) and my example below:

Create an index:

```auto
PUT /rtest
{
   "mappings": {
      "num": {
         "properties": {
            "number": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

```

Index two documents:

```auto
PUT /rtest/num/1
{
  "number": "10AB00000001"
}
PUT /rtest/num/2
{
  "number": "10AB00000001:count"
}

```

Search

```auto
GET /rtest/num/_search
{
   "query": {
      "regexp": {
         "number": {
            "value": "10AB[0-9]{8}"
         }
      }
   }
}

```

This returns one hit: "10AB00000001", just as we want:

```auto
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "rtest",
            "_type": "num",
            "_id": "1",
            "_score": 1,
            "_source": {
               "number": "10AB00000001"
            }
         }
      ]
   }
}

```

For a more formal description of the supported syntax, see [Lucene's Javadoc of the class Regexp](https://lucene.apache.org/core/5_5_0/core/org/apache/lucene/util/automaton/RegExp.html).

Daniel

---

<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 22, 2016, 10:25pm UTC](https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030/4 "2016-03-22T22:25:14Z")

</div>

Ahh nice!

---

<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:06pm UTC](https://discuss.elastic.co/t/regexp-support-for-matching-at-the-beginning-and-the-end/45030/5 "2017-07-05T23:06:05Z")

</div>


