# Regex lookahead in painless

**URL:** <https://discuss.elastic.co/t/regex-lookahead-in-painless/328268>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 22, 2023, 3:18pm UTC](https://discuss.elastic.co/t/regex-lookahead-in-painless/328268 "2023-03-22T15:18:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HansPeterSloot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hanspetersloot/32/51132_2.png) [@HansPeterSloot](https://discuss.elastic.co/u/HansPeterSloot)\
**Post date:** [March 22, 2023, 3:18pm UTC](https://discuss.elastic.co/t/regex-lookahead-in-painless/328268/1 "2023-03-22T15:18:45Z")

</div>

Is it possible to use regex with lookahead in Painless?  
I want to match a pattern in a string starting with a certain expression and ending before a certain expression.

---

<div class="post-metadata">

**Author:** ![William\_Brafford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_brafford/32/51559_2.png) [@William\_Brafford](https://discuss.elastic.co/u/William_Brafford)\
**Post date:** [March 22, 2023, 4:10pm UTC](https://discuss.elastic.co/t/regex-lookahead-in-painless/328268/2 "2023-03-22T16:10:16Z")

</div>

I believe Painless supports what Java supports, which includes lookaheads. For example,

```auto
"look ahead operator" =~ /look (?=ahead).+/

```

is a match in Painless.

You do need to be aware of the Elasticsearch settings that, by default, limit regex complexity: [Elasticsearch regex circuit breaker settings](https://www.elastic.co/guide/en/elasticsearch/reference/current/circuit-breaker.html#regex-circuit-breaker).

---

<div class="post-metadata">

**Author:** ![HansPeterSloot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hanspetersloot/32/51132_2.png) [@HansPeterSloot](https://discuss.elastic.co/u/HansPeterSloot)\
**Post date:** [March 23, 2023, 12:17pm UTC](https://discuss.elastic.co/t/regex-lookahead-in-painless/328268/3 "2023-03-23T12:17:25Z")

</div>

Yes it works  
But I struggled with the code.

It goes like this:

```auto

 def mm = /request=.*(?=response)/.matcher(message);
 if (mm.find()) {
            ctx._source['mm'] = mm.group(0);
 }

```

This matches "request.\*" until the string "response" is found.

---

<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:** [April 20, 2023, 12:18pm UTC](https://discuss.elastic.co/t/regex-lookahead-in-painless/328268/4 "2023-04-20T12:18:04Z")

</div>

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