Extract failure part code from unstructured stringHi,
I want to extract failure part codes from the unstructured string below using Regex in JSL. But I was not success to get it because it is complicated.
Repair T126 & PA21. retest pass
change L6C, FTNG4 reroute for retest
Test 1 failure part P12; M531, Z6A2. Test 2 failure part Z100, RT98; BTGH9. Rework all the parts
failure parts replaced (K3; LG69; AK4T). Proceed to next test
damaged part YT10 rework the part, proceed to next process
Required outcome in column "Failure Part Code" should look something this

Any advice and help would be much appreciated.
Thank you
Automation and ScriptingRe: Extract failure part code from unstructured stringCapturing multiple things with single regex isn't that simple in JMP as there isn't global matching possibility (
This fairly simple regex does capture first codes for you
Names Default To Here(1);
dt = Open("$DOWNLOADS/data1.jmp");
failure_pattern = "[A-Z]+\d+[A-Z]?";
Eval(EvalExpr(
dt << New Column("A", Character, Nominal, << Formula(
Regex(:Repair Details, Expr(failure_pattern));
));
));
And then pattern matching mess (can be most likely made much better)
Names Default To Here(1);
dt = Open("$DOWNLOADS/data1.jmp");
str = "failure parts replaced (K3; LG69; AK4T). Proceed to next test";
dt << New Column("A", Character, Nominal, << Formula(
failure_pattern = "([A-Z]+\d+[A-Z]?){1,}";
found_list = {};
Pat Match(:Repair Details,
Pat Any(", (") + Pat Regex(failure_pattern) >> match + Pat Any(" .;,)") + Pat Test(
Insert Into(found_list, match);
0
)
);
Concat Items(found_list, ", ");
)); Re: Extract failure part code from unstructured stringI recently discovered a JMP function/option that can handle this pretty easily. New Column by Text Matching.
Try this script or use the utility under Cols->Utilities with your Repair Details column selected.
New Column by Text Matching(
Column( :Repair Details ),
Set Regex( Custom( Title( "parts" ), Regex( "([A-Z]+\d+[A-Z]?)" ), Result( "\[\1]\" ), ) ),
Output Column Name( "Failure Part Code" )
);

Re: Extract failure part code from unstructured stringThanks Jarmo. This is great!
Re: Extract failure part code from unstructured stringThanks for sharing this function "New Column by Text Matching". It helps.