Using regex to create a substring

Is there a way to use regex and apply it to an existing string in order to get a substring?

Say I have a bunch of strings like this. They are not in a list but individual strings.
ABC 2012-10-01
DEF 2013-07-10
2018-01-21
ABG 2015-10-30A
EDF 2019-01-19B
ABC/DEF/GEH 2019-01-19A

I want to be able to convert each individual strings to the following format using the regex here /(\d\d\d\d)-(\d\d)(-\d\d[a-zA-Z])?/

Tested it here and it works

2012-10-01
2013-07-10
2018-01-21
2015-10-30A
2019-01-19B
2019-01-19A

However, I’m not sure how to do it in the WF studios, tried experimenting with the custom script action but I’m not able to come up with a solution. Any thoughts?

Bumping this for visibility. I’ve resorted to using substrings to deal with this problem but it’s not the best solution because I wasn’t able to account for some string cases.

Hi @tinwy try using these methods in a custom script

Thanks!! The article really helped.

@CustomScriptAction(
        input = ['target_str'],
        output = 'result_str'
    )

    def regexSubstring() {
        def regex = /(\d\d\d\d)-(\d\d)(-\d\d[a-zA-Z])?/
        def matcher = (targer_str =~ /$regex/)
        result_str = RString.of(matcher[0][0])
    }

Note how I to access the matcher object depends on the number of capturing groups inside the regex that was used. In my case, there were 3 capturing groups denoted by the brackets ( … ).

matcher[0][0] will refer to everything that matched the regex,
matcher[0][1] will refer to the first capture in this match.
matcher[0][2] will refer is the second capture in this match.
and so on.

2 Likes

Hi @tinwy Can i encode or decode a string value using custom script .

if yes , then please let me know it very important for me.

please…

Hi @Jitendriya

You can definitely do it using groovy. This link might help you with what you need.

2 Likes

So i directly use this code in workfusion custom script for encoding and decoding.