# Logstash Ruby Code string

**URL:** <https://discuss.elastic.co/t/logstash-ruby-code-string/184231>\
**Category:** Logstash\
**Created:** [June 4, 2019, 4:34pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-string/184231 "2019-06-04T16:34:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan01](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jonathan01](https://discuss.elastic.co/u/jonathan01)\
**Post date:** [June 4, 2019, 4:34pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-string/184231/1 "2019-06-04T16:34:11Z")

</div>

I am new to this and I am a little stuck.  
I have a input from metric beats

"windows" =\> {  
"perfmon" =\> {  
"ica" =\> {  
"output\_pn\_bandwidth" =\> 0,  
"name" =\> "ICA-CGP 33 (jgillman)"  
}  
}

I need to get the user name from the name field. The user name will change it could be jgillman or r.little .... etc  
In my logstash conf file I put  
mutate {  
add\_field =\> { "user" =\> "%{[windows][perfmon][ica][name]}"}  
}

```
ruby {
    code => "event.set('USER2', event.get('user')[12..30])"
}

```

the out put that I get is "USER2" =\> "jgillman)"  
the problem is the last ")"  
how would I get rid of that?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [June 4, 2019, 5:11pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-string/184231/2 "2019-06-04T17:11:37Z")

</div>

I would not use ruby for that. Instead try

```
mutate { gsub => ["user", ".*\((.*)\)", "\1"] }
```

---

<div class="post-metadata">

**Author:** ![jonathan01](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jonathan01](https://discuss.elastic.co/u/jonathan01)\
**Post date:** [June 4, 2019, 5:26pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-string/184231/3 "2019-06-04T17:26:44Z")

</div>

thanks so much that worked

---

<div class="post-metadata">

**Author:** ![jonathan01](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jonathan01](https://discuss.elastic.co/u/jonathan01)\
**Post date:** [June 4, 2019, 5:31pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-string/184231/4 "2019-06-04T17:31:22Z")

</div>

I am new to this could you please explain the code

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [June 4, 2019, 5:38pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-string/184231/5 "2019-06-04T17:38:34Z")

</div>

It is .\* (anything) followed by a literal (, followed by a capture group () that contains .\* (anything), followed by a literal ). That matches the entire string, and the gsub replaces that (the entire string) with \1 (the value of the first capture group, which is everything inside the ( and )).

---

<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 2, 2019, 5:38pm UTC](https://discuss.elastic.co/t/logstash-ruby-code-string/184231/6 "2019-07-02T17:38:36Z")

</div>

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