# Regexp Query - How to detect the beginning and the finish of a string?

**URL:** <https://discuss.elastic.co/t/regexp-query-how-to-detect-the-beginning-and-the-finish-of-a-string/167083>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 9:03am UTC](https://discuss.elastic.co/t/regexp-query-how-to-detect-the-beginning-and-the-finish-of-a-string/167083 "2019-02-05T09:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wassim\_Ben\_Amor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wassim_ben_amor/32/39799_2.png) [@Wassim\_Ben\_Amor](https://discuss.elastic.co/u/Wassim_Ben_Amor)\
**Post date:** [February 5, 2019, 9:03am UTC](https://discuss.elastic.co/t/regexp-query-how-to-detect-the-beginning-and-the-finish-of-a-string/167083/1 "2019-02-05T09:03:16Z")

</div>

Hello,  
The documentation says :  
If you want the regexp pattern to start at the beginning of the string or finish at the end of the string, then you have to _anchor_ it specifically, using `^` to indicate the beginning or `$` to indicate the end.  
But if we put for example this document

> ```
> PUT test50/_doc/1 
> {
> "request": "test"
> }
> 
> ```

and then lauch a query with this regexp :

> ```
> GET test50/_search
> {
> "query": {
> "regexp": {
> "request.keyword": "(.* |^)test( .*|$)"
> }
> }
> }
> 
> ```

the object is to find the word in the beggining, middle, or the finish of the string.  
This request dosen't match the document.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 5, 2019, 9:27am UTC](https://discuss.elastic.co/t/regexp-query-how-to-detect-the-beginning-and-the-finish-of-a-string/167083/2 "2019-02-05T09:27:01Z")

</div>

Note that you don't need to use both the code format icon and the citation icon. Code format is perfect.

I can't answer on regex as I have hard time to write regex myself.  
But are you aware that this is a very very slow method to find a substring in a text?

If this is something you intend to run on a small dataset or only once, then that could be ok, otherwise I'd suggest to use a ngram analyzer at index time instead and then use a Term query.

---

<div class="post-metadata">

**Author:** ![Wassim\_Ben\_Amor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wassim_ben_amor/32/39799_2.png) [@Wassim\_Ben\_Amor](https://discuss.elastic.co/u/Wassim_Ben_Amor)\
**Post date:** [February 5, 2019, 9:31am UTC](https://discuss.elastic.co/t/regexp-query-how-to-detect-the-beginning-and-the-finish-of-a-string/167083/3 "2019-02-05T09:31:43Z")

</div>

Thank you for your reply.  
The code format dosen't work. it only worked when activating quote & code format.  
Well, the dataset is large, and I have a big set of regexes that I should apply to the dataset.  
But my problem is, how can we detect the beginning and the finish of a string normally, like a perl regex with ^ and $ .

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 5, 2019, 10:01am UTC](https://discuss.elastic.co/t/regexp-query-how-to-detect-the-beginning-and-the-finish-of-a-string/167083/4 "2019-02-05T10:01:32Z")

</div>

> [@Wassim\_Ben\_Amor](#):
>
> The code format dosen't work. it only worked when activating quote & code format.

It's because you did not add a blank line after:

> But if we put for example this document

And before

> and then lauch a query with this regexp

Back to the questions:

> Well, the dataset is large

So you will run into performance troubles. But if this is ok for you, then do it.

> But my problem is, how can we detect the beginning and the finish of a string normally, like a perl regex with ^ and $ .

As I said, I don't know as I'm not a regex expert. Someone else might help.

---

<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 5, 2019, 10:01am UTC](https://discuss.elastic.co/t/regexp-query-how-to-detect-the-beginning-and-the-finish-of-a-string/167083/5 "2019-03-05T10:01:46Z")

</div>

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