cancel
Showing results for 
Search instead for 
Did you mean: 

How to split a string based on Keyword?

Former Member
0 Kudos

Hello Folks,

I have a IDOC to file scenario.

In IDOC one of the segment has values available in the format MOT=2;POL=;USPOE=3801;FC=70;W=230;CCAI=2013.

The values are separated by semicolon ;.

Now I have to send each of these values to separate field of Output File.

For example;

2 to field1,

Blank to field2

3801 to field3

70 to field4

230 to field5 and

2013 to field6

Also, please note here the string in IDOC is not a fixed length one. It depends on whether the End User fill some value or not According field is populated

also UPSOE can be 3 digit or 4 digit value and FC can come as 70.00 or 70.0 or 70 depending on how user fill the value.

So basically the search in the string is based on keywords like MOT, POL etc.

Please let me know how to achieve this?

Regards,

Sachi

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

you can use regex - user defined function with 3 input parameters.follow the below code.. For each fields you just need to call the UDF and pass the required params

Assign under Import..

java.util.regex.Matcher

java.util.regex.Pattern

Parameter

sInput (takes Idoc input data string as MOT=2;POL=;USPOE=3801;FC=70;W=230;CCAI=2013)

Sval1 (define as constant for each target field as MOT= or FC= or USPO )

Sval2 = ;  (Constant ; )

UDF

Pattern p = Pattern.compile(Pattern.quote(sVal1) + "(.*?)" + Pattern.quote(sVal2));

Matcher matcher = p.matcher(sInput);

if (matcher.find()) {

  return matcher.group(1);

}else

return "false";

Former Member
0 Kudos

Thanks a lot Ramesh!

It works well. I will bookmark it.

Please let me know how to identify the last value CCAI=2013, as the delimiter is not semi-colon??

I tried using blank but it doesn't work.

Regards,

Sachi

Former Member
0 Kudos

ok easy fix is to just concat ; at the end with input idoc data...

Answers (0)