Replace string using regular expression in KETTLE

2019-09-06 06:25发布

问题:

I would like to use regular expression for replacing a certain pattern in the Kettle. For example, AAAA >5< BBBB, I want to replace this with AAAA 555 BBBB. I know how to find the pattern, but I am not sure how to replace that with new string. The one thing I have to keep is that I have to find pattern together ><, not separately like > or < because there is another pattern <5>.

回答1:

You can use the "Replace in String" step in a transformation.

Set use RegEx to "Y", type your regex on the Search box, with capturing groups if necessary, and the replacement string in the replacement box, referring to capture groups as $1, $2, ...

It'll replace all occurrences of the regex in the original string.

If the Out Stream field is ommitted, it'll overwrite the In stream field.



回答2:

If you want the pattern >\d< replaced by a triple of the found digit, you can use Replace-In-String in regex mode:

Search:     (.*)(>(\d)<)(.*)
Replace:    $1$3$3$3$4

If you want all such patterns treated the same:

Search:     (>(\d)<)
Replace:    $2$2$2

EDIT due to your improved requirement

Since you intend to convert your "simple" markup to a more HTML-like markup, you better use a User-Defined-Java-Expression. Also, you must avoid to reintroduce simple markup when replacing repeatedly.



标签: regex kettle