Match and replace in a string

Match and replace in a string

Hi,
How can I match multiple characters and replace in a single function. The requirement is that I need to match newline and \n and replace it with blank.I tried as below but is there a way to simplify this as I would also be required to additionally replace \001 and ^A character in the string and the function will become big.
StringHandling.EREPLACE(StringHandling.EREPLACE(row2.LOC_Number,"\\\\n",""),"\n","")
Thanks.
Moderator

Re: Match and replace in a string

Hi,
Could you please elaborate your case with an example with input and expected output values? What your input string looks like?
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Four Stars

Re: Match and replace in a string

Hi,
Have you checked tReplace component? You can add as many conditions you need.
https://help.talend.com/search/all?query=tReplace&content-lang=en
Vaibhav

Re: Match and replace in a string

Hi Sabrina,
I am parsing an XML and would need to filter out any newline characters(\n) or null characters(\001) coming between the tags as the target schema is delimited based on this.So I am trying to see better options to replace these characters as I need to do this validation on every tag I read and I will be reading around 150 tags.
Is there a better way other than the one I am doing above to achieve it.
Also is it possible to do it directly on the XML parse component(I am using tFileInputXml component).

Hi Vaibhav,
Thanks for the response but since I already have tMap I thought of using the function inside tMap instead of adding a new component.
But on a case where I do not have a tMap do tReplace provide any advantage over tMap.
Thanks.
Moderator

Re: Match and replace in a string

Hi,
You can use
 Replace(string s1, string s2) or ReplaceAll(string s1, string s2)

method in tMap expression.
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Four Stars

Re: Match and replace in a string

My view is, when talend have developed components for performing specific tasks, there is always an edge over custom expressions which would replace components. More over the manual function which we write need to be tested and verified. However, when we use standard component, we need to do very less to make it work.
Another advantage of component is that you need not be aware of how to use java... which is difficult for business people.
tMap is a very versatile component... comparing tMap with tReplace would depend upon what task you want to accomplish. For replacement with different chars for different chars/columns, I would prefer tReplace over tMap....

Vaibhav

Re: Match and replace in a string

Thanks Sabrina/Vaibhav.
I tried using tReplace component but I was able to replace only one character for a field in the component. Is it that I am missing to see something as I would be required to replace multiple characters in the string.
When I use the functions I can nest and do the replacement one by one but here I need to replace "\001" and "\n" from a column and if both appears on a column both need to be replaced.
Please let me know if I can achieve the same with tReplace component.
One Star

Re: Match and replace in a string

Im having the same issue of replacing a dot(.) and plus(+) symbol in one column.I am able to replace one symbol at a time but does java accept a nested replace function like in sql 
eg: - Str.replaceall(("ABC",""),"\+","")