# Painless regex does not match

**URL:** <https://discuss.elastic.co/t/painless-regex-does-not-match/134775>\
**Category:** Elasticsearch\
**Created:** [June 6, 2018, 9:45am UTC](https://discuss.elastic.co/t/painless-regex-does-not-match/134775 "2018-06-06T09:45:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santhosh\_ic](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@Santhosh\_ic](https://discuss.elastic.co/u/Santhosh_ic)\
**Post date:** [June 6, 2018, 9:45am UTC](https://discuss.elastic.co/t/painless-regex-does-not-match/134775/1 "2018-06-06T09:45:48Z")

</div>

Hi,

There are documents with content "..............6; ResponseTime: 0ms; RequestHeaders: ............." I have a scripted field with painless script,

```
    if(!doc['Message.keyword'].empty) {
 def m = /^(.*?)(?=ResponseTime:)ResponseTime: (([0-9]*[.])?[0-9]+)ms;(.*)$/.matcher(doc['Message.keyword'].value);
if(m.matches()) {
return m.group(2)
} else {
return "no match"
}
} else {
return "empty doc"
}

```

I have tried m.find and m.lookingAt as well.

script for m.find

```
if(!doc['Message.keyword'].empty) {
def m = /ResponseTime: ([0-9]+)ms;/.matcher(doc['Message.keyword'].value);
if(m.find()) {
return m.group(1)
} else {
return "no match"
}
} else {
return "empty doc"
}

```

This is returning "empty doc". No way it should have returned "empty doc" as the docs are there with content. For any other value for regex like " invalid user:\s{1,}([a-zA-Z0-9]+)" works perfectly with the "invalid user: admin" content.

From other regex tools, I can see that the expression is correct.

Same is the case with this script as well.

```
    if(!doc['Message.keyword'].empty) {
def m = /^\s{0,}Payload xml is\s{0,}(.*)$/.matcher(doc['Message.keyword'].value);
if(m.matches()) {
return m.group(1)
} else {
return "no match"
}
} else {
return "no match"
}

```

Here is the Java program that works for same regex

```
    	Pattern p = Pattern.compile("ResponseTime: ([0-9]+)ms;");
		 Matcher m = p.matcher("dfgadgfhadgfhdfhgsjhfgjhdfhdsfgdhf ResponseTime: 0ms; dfbnbgngn sbdmf adf mansdfb nbdfm mfbnas");
		 boolean b = m.find();

		 if (b == true) {
			 System.out.println("Regex matched " + m.group(1));
		 } else {
			 System.out.println("Regex did not mactch");
		 }
		
		System.out.println("Hello Java");
	}
Output is "Regex matched 0
Hello Java"

```

Any help is appreciated.

---

<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 4, 2018, 9:45am UTC](https://discuss.elastic.co/t/painless-regex-does-not-match/134775/2 "2018-07-04T09:45:55Z")

</div>

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