# Working with regex

**URL:** <https://community.icinga.com/t/working-with-regex/7138>\
**Category:** Icinga Director\
**Tags:** director\
**Created:** [April 16, 2021, 10:20am UTC](https://community.icinga.com/t/working-with-regex/7138 "2021-04-16T10:20:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robse](https://community.icinga.com/letter_avatar_proxy/v4/letter/r/5daacb/32.png) [@robse](https://community.icinga.com/u/robse)\
**Post date:** [April 16, 2021, 10:20am UTC](https://community.icinga.com/t/working-with-regex/7138/1 "2021-04-16T10:20:15Z")

</div>

Hi together,

while importing my computerobjects from AD, i want to extract a string between two brackets to use this for another Variable. Unfortunately, my regex doesnt work and i have no idea why.

This is my regex: ([(.\*?)])  
This is a example string: [FS] Fileserver (Windows)  
Method is: RegexSplit

I want to extract the string ‘FS’ between the square brackets.

Does somebody has any idea?

Regards,  
Robert

---

<div class="post-metadata">

**Author:** ![dgoetz](https://community.icinga.com/user_avatar/community.icinga.com/dgoetz/32/917_2.png) [@dgoetz](https://community.icinga.com/u/dgoetz)\
**Post date:** [April 16, 2021, 10:52am UTC](https://community.icinga.com/t/working-with-regex/7138/2 "2021-04-16T10:52:48Z")

</div>

RegexSplit is the wrong modifier (splitting into an array using regex), use Regular Expression based replacement instead.  
As Regex use `/\[(.*)\].*/` which is everything between the brackets in capture group 1 but matches everything.  
As Replacement use `\1` so it replaces everything with the capture group 1.

---

<div class="post-metadata">

**Author:** ![robse](https://community.icinga.com/letter_avatar_proxy/v4/letter/r/5daacb/32.png) [@robse](https://community.icinga.com/u/robse)\
**Post date:** [April 16, 2021, 12:06pm UTC](https://community.icinga.com/t/working-with-regex/7138/3 "2021-04-16T12:06:03Z")

</div>

Hi Dirk,

Fantastic, it works. Now i have to filter Objects without brackets, like this one: “RD-Lizenzserver”.  
Can the first regex be expanded or should i create a second modification-rule with regex-split?

Regards,  
Robert

---

<div class="post-metadata">

**Author:** ![dgoetz](https://community.icinga.com/user_avatar/community.icinga.com/dgoetz/32/917_2.png) [@dgoetz](https://community.icinga.com/u/dgoetz)\
**Post date:** [April 16, 2021, 1:54pm UTC](https://community.icinga.com/t/working-with-regex/7138/4 "2021-04-16T13:54:44Z")

</div>

Depends on what you exactly need.

My try would be an or-clause `/\[(.*)\].*|.*/`. This will match everything when there are no brackets in the or-clause and then capture group 1 should be empty, so my hope would be it would replace everything with an empty string.

Would the empty string be ok? If not and `null` is needed, it gets tricky as you would need to import values in a datalist and use map as modifier.

---

<div class="post-metadata">

**Author:** ![robse](https://community.icinga.com/letter_avatar_proxy/v4/letter/r/5daacb/32.png) [@robse](https://community.icinga.com/u/robse)\
**Post date:** [April 16, 2021, 8:36pm UTC](https://community.icinga.com/t/working-with-regex/7138/5 "2021-04-16T20:36:30Z")

</div>

Hi Dirk,

I filter out the empty String with `(^[0-9]*$|^NULL$)`. Now everythink works fine.

Thank you.

Kind regards  
Robert
