# Extract substring from field

**URL:** <https://discuss.elastic.co/t/extract-substring-from-field/180910>\
**Category:** Logstash\
**Created:** [May 14, 2019, 3:53am UTC](https://discuss.elastic.co/t/extract-substring-from-field/180910 "2019-05-14T03:53:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Kaspar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_kaspar/32/44443_2.png) [@Jan\_Kaspar](https://discuss.elastic.co/u/Jan_Kaspar)\
**Post date:** [May 14, 2019, 3:53am UTC](https://discuss.elastic.co/t/extract-substring-from-field/180910/1 "2019-05-14T03:53:36Z")

</div>

Hello,

I have typical scenatio. I have field that contains distinguished name. I need to extract common name and copy it to new field. I have working regex but i really dont know how to use it in match.

Existing field  
event\_data.MemberName : CN=Admin,OU=Admins,OU=GLOBAL,DC=TEST,DC=LOCAL

wanted field:  
CommonName : Admin

I have regex tested on [rubular.com](http://rubular.com)

`^CN=([^,]*).*`

Thanks a lot.

Jan

---

<div class="post-metadata">

**Author:** ![saif3r](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@saif3r](https://discuss.elastic.co/u/saif3r)\
**Post date:** [May 14, 2019, 7:18am UTC](https://discuss.elastic.co/t/extract-substring-from-field/180910/2 "2019-05-14T07:18:14Z")

</div>

Have you tried Oniguruma?

For regex based extraction, i'm using something like this:

```
grok {
	match => {
		"source_field" => "(?<destination_field>^CN=([^,]*).*)"
	}
}

```

I'm not sure if that will work, but I'd start with that.

---

<div class="post-metadata">

**Author:** ![Jan\_Kaspar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_kaspar/32/44443_2.png) [@Jan\_Kaspar](https://discuss.elastic.co/u/Jan_Kaspar)\
**Post date:** [May 14, 2019, 8:16am UTC](https://discuss.elastic.co/t/extract-substring-from-field/180910/3 "2019-05-14T08:16:29Z")

</div>

Hi Saif3r,

This is actualy what I tried. this will copy all DN to new field.

```
grok {
	match => {
		"[event_data][MemberName]" => "(?<MemberNameShort>^CN=([^,]*).*)"
	}
}

```

result:

```
MemberNameShort :	CN=Admin,OU=Global Admins,OU=GLOBAL,DC=TEST,DC=LOCAL
event_data.MemberName	: CN=Admin,OU=Global Admins,OU=GLOBAL,DC=TEST,DC=LOCAL

```

regex match but not use match group.

Jan

---

<div class="post-metadata">

**Author:** ![saif3r](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@saif3r](https://discuss.elastic.co/u/saif3r)\
**Post date:** [May 14, 2019, 8:40am UTC](https://discuss.elastic.co/t/extract-substring-from-field/180910/4 "2019-05-14T08:40:13Z")

</div>

So your issue is related to regex expression, not the config itself.  
Try this instead:

> (?\<=CN=)\w\*[^,]

Result:

 ![regex](https://us1.discourse-cdn.com/elastic/original/3X/d/f/df160435245b7ceac21cbedf16f6edbbc4bf2aeb.png)

---

<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:** [June 11, 2019, 8:40am UTC](https://discuss.elastic.co/t/extract-substring-from-field/180910/5 "2019-06-11T08:40:21Z")

</div>

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