Using StringHandling methods to loop through string and extract values

One Star

Using StringHandling methods to loop through string and extract values

Hi, I have a quick question. In a delimited FileInput object I have multiple fields. In one of those fields I have a four-piece comma-delimited string that's separated by ampersands, like this...

2,a11,0,000&3,a22,0,111&5,a33,0,222

How would I use the StringHandling methods to extract only the second portion of each string (a11,a22,a33)? Or would I use other objects? Or is there a way to use regular Java String methods instead of the StringHandling methods (and if so, how?)

Thanks for your help!
One Star

Re: Using StringHandling methods to loop through string and extract values

What does the target look like?
One Star

Re: Using StringHandling methods to loop through string and extract values

The target output should be one string looking like this: a11 a22 a33.
One Star

Re: Using StringHandling methods to loop through string and extract values

This uses regular expressions.

Put this code in a Talend routine.

	public static String getIds(String _s, String _alphaKey, int _ndigits) {
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(_alphaKey + "[0-9]+");
Matcher m = p.matcher(_s);
while( m.find() ) {
sb.append( m.group(0) );
sb.append(" ");
}
return sb.toString();
}

Call the routine in a tMap like this (assuming that IdRoutines was added for the above code)

             row1.idField = IdRoutines.getIds(row2.idField, "a", 2)

You'll also need to import 2 standard Java libraries
            import java.util.regex.Matcher;
import java.util.regex.Pattern;

See this article for instructions on adding Java code to a routine: http://bekwam.blogspot.com/2011/02/user-defined-functions-with-talend-open.html.
One Star

Re: Using StringHandling methods to loop through string and extract values

You, sir (or madam), have saved my ass. Thank you thank you thank you! And now I know about the magic of Talend routines. That will help A LOT as I continue beginning to work with this software. If I have any other questions I'll let you know. Thanks again!
One Star

Re: Using StringHandling methods to loop through string and extract values

Also, a note for everyone else out there, be sure to right-click on your job and select 'Setup Routine Dependencies' - this is the only way your newly-created routines will be executed correctly by the job.
Seventeen Stars

Re: Using StringHandling methods to loop through string and extract values

hi,

thanks to point that feature jmoe Smiley Wink
regards

laurent

2019 GARNER 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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Have you checked out Talend’s 2019 Summer release yet?

Find out about Talend's 2019 Summer release

Blog

Talend Summer 2019 – What’s New?

Talend continues to revolutionize how businesses leverage speed and manage scale

Watch Now

6 Ways to Start Utilizing Machine Learning with Amazon We Services and Talend

Look at6 ways to start utilizing Machine Learning with Amazon We Services and Talend

Blog