replace whole string by matching part of it

Highlighted
Six Stars

replace whole string by matching part of it

Hi. I have a set of strings like below.

"helloworldabc"

"o helloworld"

"helloworld+++++"

"helloworld + 3"

"helloworld10"

"polo-world"

"polo-world+"

"polo-world++++++"

"polo-world15"

 

I want to match all the strings that have substring "helloworld" and replace the whole string by Hello-World without having the numbers, alphabets, symbols before and after. And do the same by matching the strings that have "polo-world" and replace by "Polo-World" along with removing anything before and after polo-world. Can someone suggest a way to do on treplace or tmap?

 


Accepted Solutions
Highlighted
Sixteen Stars
Sixteen Stars

Re: replace whole string by matching part of it

You may also use a regex to cover the whole case:

(row1.myColumn.replaceAll("^.*helloword.*$", "Hello World")).replaceAll("^.*polo-world.*$", "Polo-World")

Should work.

 


TRF

View solution in original post


All Replies
Highlighted
Thirteen Stars

Re: replace whole string by matching part of it

on tmap.
on the right side you can push … button on filter or on expression.
on Expression builder select String Handling function ereplace
it's what you are looking for.
StringHandling.EREPLACE(row1.col1,"helloworld","Hello-World")

Francois Denis

Tag as "solved" for others! Kudos to thanks!

Highlighted
Six Stars

Re: replace whole string by matching part of it

@fdenis, it doesn't work. I need to remove anything before and after the words "helloworld"

Highlighted
Six Stars

Re: replace whole string by matching part of it

@fdenis, I have to replace the whole string now. EReplace changed only "helloworld" to "Hello-World" but the strings "helloworld++++", "helloworld10", "ohelloworld" etc didn't lose their "+++", "10" and "o"

Highlighted
Community Manager

Re: replace whole string by matching part of it

You need to use indexOf() and if the index is greater than -1, replace the whole String. So something like this....

row1.myColumn.indexOf("Hello World")>-1 ? "Hello World" : row1.myColumn

The above can be used practically anywhere and says "If Hello World appears anywhere in my column, set the value I am returning to "Hello World", otherwise return the value that came in the column"

Highlighted
Sixteen Stars
Sixteen Stars

Re: replace whole string by matching part of it

You may also use a regex to cover the whole case:

(row1.myColumn.replaceAll("^.*helloword.*$", "Hello World")).replaceAll("^.*polo-world.*$", "Polo-World")

Should work.

 


TRF

View solution in original post

Highlighted
Six Stars

Re: replace whole string by matching part of it

@TRF and @rhall_2_0 Thanks a lot! Both work well and can use as solutions.

Highlighted
Sixteen Stars
Sixteen Stars

Re: replace whole string by matching part of it

You're welcome

TRF

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog