# Ruby filter to extract different data elements from a string based on pattern

**URL:** https://discuss.elastic.co/t/ruby-filter-to-extract-different-data-elements-from-a-string-based-on-pattern/285717
**Category:** Logstash
**Created:** [October 1, 2021, 11:46pm UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-different-data-elements-from-a-string-based-on-pattern/285717 "2021-10-01T23:46:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Indigo\_Star](https://avatars.discourse-cdn.com/v4/letter/i/ba8739/32.png) [@Indigo\_Star](https://discuss.elastic.co/u/Indigo_Star)
#### Post date: [October 1, 2021, 11:46pm UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-different-data-elements-from-a-string-based-on-pattern/285717/1 "2021-10-01T23:46:50Z")

</div>

Hi,

I need to extract data from a field transaction which contains value as:

Transaction =  
"CHE\_ECSTATE '-259297525' 19802619:-259297525{0}BEACH.TCN4(TCN4)ECN4/UTR023/ECTBR@20210929.162523.985

I need fields like  
CHE\_ECSTATE='-259297525'  
key1 = 19802619  
key2=259297525  
version={0}  
name=BEACH.TCN4(TCN4)ECN4/UTR023/ECTBR  
trans-time=20210929.162523.985

Then convert the trans time to the datetime

Thanks

---

<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: [October 2, 2021, 12:16am UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-different-data-elements-from-a-string-based-on-pattern/285717/2 "2021-10-02T00:16:05Z")

</div>

Why do you want to do that using a ruby filter instead of grok?

---

<div class="post-metadata">

### Author: ![Indigo\_Star](https://avatars.discourse-cdn.com/v4/letter/i/ba8739/32.png) [@Indigo\_Star](https://discuss.elastic.co/u/Indigo_Star)
#### Post date: [October 2, 2021, 12:32am UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-different-data-elements-from-a-string-based-on-pattern/285717/3 "2021-10-02T00:32:38Z")

</div>

Thanks @Badger for your suggestion.

I thought ruby would be more better but i figured this pattern works for grok for my transaction

(?\s\*)\s\*'(?._)'\s_(?\d+):-\s\*(?\d+){\d+}(?._)((?._)._)(?._)@(?.\*)

Thanks

---

<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: [October 30, 2021, 12:33am UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-different-data-elements-from-a-string-based-on-pattern/285717/4 "2021-10-30T00:33:16Z")

</div>

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