# Painless script regex

**URL:** <https://discuss.elastic.co/t/painless-script-regex/267419>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 16, 2021, 8:16pm UTC](https://discuss.elastic.co/t/painless-script-regex/267419 "2021-03-16T20:16:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris\_Boris](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boris_boris/32/85640_2.png) [@Boris\_Boris](https://discuss.elastic.co/u/Boris_Boris)\
**Post date:** [March 16, 2021, 8:16pm UTC](https://discuss.elastic.co/t/painless-script-regex/267419/1 "2021-03-16T20:16:06Z")

</div>

I have a event.original field -\> type String.  
"2021-03-15T16:11:12.333659+00:00 10.10.252.2 id=firewall sn=xx time=2021-03-15 16:11:12 UTC fw=8.8.8.8 pri=5 c=256 m=38 msg=ICMP packet dropped due to Policy proto=icmp type=3 icmpCode=3"

I want to extract scripted field with regex.

```
if(!doc['event.original'].empty)
 {
 String s = doc['event.original'].value;
 String clr = s.replace("\"","");

 if ( clr =~ /(?<=msg=)\w+/ ) {
  return "matc"
 } 
 else {
  String test = doc['event.original'].value;
  String test1 = test.replace("\"","");
  return test1 
 }
}

```

This script returned "matc"  
But when i try to return value with:

```
if(!doc['event.original'].empty)
 {
 String s = doc['event.original'].value;
 String clr = s.replace("\"","");
 def m = /(?<=msg=)\w+/.matcher(clr);

 if ( m.matches() ) {
  return "matc"
 } 
 else {
  return "not matched"
 }
}

```

This returns "not matched"  
so i can't use scriptied field with value of "ICMP packet dropped due to Policy"

---

<div class="post-metadata">

**Author:** ![Boris\_Boris](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boris_boris/32/85640_2.png) [@Boris\_Boris](https://discuss.elastic.co/u/Boris_Boris)\
**Post date:** [March 17, 2021, 12:32pm UTC](https://discuss.elastic.co/t/painless-script-regex/267419/2 "2021-03-17T12:32:14Z")

</div>

problem resolved by using m.find()

---

<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 14, 2021, 12:32pm UTC](https://discuss.elastic.co/t/painless-script-regex/267419/3 "2021-04-14T12:32:22Z")

</div>

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