search and replace

One Star

search and replace

How to replace the string if any text contains some words it has to replace with some value
For example
In my csv file i have two columns and multiple rows with pipe delimited format
Pms|url
?Name?,?sno?|http://localhost:8080/talend?Name=?Name?&sno=?sno?
?Name?,?sno?,?address?|http://localhost:8080/talend?Name=?Name?&sno=?sno?&address=?address?
what i need is it has to search and replace the url column whenever the url content contains with pms columns it has to replace with some value
my final urls has to be formed in tjava
output
========
http://localhost:8080/talend?Name="aaa"&sno=123
http://localhost:8080/talend?Name="aaa"&sno=123&address="ABCDEFG"
Six Stars

Re: search and replace

If this is of any help, If the PMS column would contain this limited number of expressions i.e. Name, Sno, Address , then you could filter out the rows which does not contain these, and then replace the expressions using tReplace component.
One Star

Re: search and replace

No, its not like limited..
Data is not fixed..
Please give me suggestions how can we approach for this..
One Star

Re: search and replace

Can anyone give me suggestions?
Six Stars

Re: search and replace

Hi,
You can split your 1st column using the delimiter (,) and store the split strings in a string array.
Then by iterating on the length of the array, you can keep replacing the first occurrences of the regular expression ?? in your column URL.
The job would look like the image uploaded.
The major part is done throug java and the code would look something like this (this code is used in (tjava_2)
String[] PMS = row1.Name.split(",");
int len = PMS.length;
System.out.println("Length of array="+len);
String str = row1.URL.toLowerCase();
for (int i=0;i<len;i++)
{
str = str.replaceFirst("////?////?",PMS);
System.out.println("PMS = "+ PMS);
//System.out.println("Partial String = "+str);
}
row1.URL = str;

You might want to check on the regular expressions that work in java (I'm not sure the regex I have used - "////?////?" would work, so please check on that one. But this code works. No matter how much data you have in column 1, all of it will be replaced in column 2)
The iteratetoflow component will have row1.URL as its value.
The javarow component is used simply to print the output.
One Star

Re: search and replace

Thanks tom.
How can i replace dynamically if i dont know which replace string comes first.
In below code you have mentioned to replace
str = str.replaceFirst("////?////?",PMS);
It means everytime we need to give some value to replace instead of that is there any otherway we can achieve this.
Six Stars

Re: search and replace

Hi,
In your given example,
Pms|url
?Name?,?sno?|http://localhost:8080/talend?Name=?Name?&sno=?sno?
?Name?,?sno?,?address?|http://localhost:8080/talend?Name=?Name?&sno=?sno?&address=?address?
I assumed that the URL strings would be in the same order as of the incoming PMS strings. Is that not so?
If the PMS string is "?Name?,?sno?,?address?", will not the URL link have them in the same order? i.e http://localhost:8080/talend?Name=?Name?&sno=?sno?&address=?address?
Because this code doesn't require a value to be given everytime. It needs the PMS string and the URL string will be replaced in that order in which the PMS string comes.
Even if you don't know which string comes first, the first string that comes would be replaced in the first slot of URL to be replaced.