Add / in the cell value

Hi Knime community, I want to add “/” after the last letter & before the 1st number in the value for values with DMCOH or DMEUH on it. For ex. my value is “DMEUHARNX110423” format should be “DMEUHARNX/110423” and if the value does not have DMCOH or DMEUH on it then it remains the same. I have the initial string manipulation formula:

regexReplace($CR$, “(DMCOH|DMEUH)(.*)$”, “$1/$2”)

but this one only put the “/” after the DMCOH or DMEUH:
1

I try other formulas like: regexReplace($CR$, “(DMEUH|DMCOH)([A-Z]+)(\d+)$”, “$1$2/$3”) but its not working on my end.

Hi @Heldyyyyy , I think you were very close, but you forgot to capture the additional non-alpha character (e.g. /) that may be contained in the DMCOH and DMEUH strings, and possibly didn’t include the extra backslash required by String Manipulation to “escape the escape” in \d

Assuming all uppercase, try this:

regexReplace($CR$, "(DMEUH|DMCOH)(.*[A-Z]+)(\\d+)$", "$1$2/$3")

or this (to avoid escape problems)

regexReplace($CR$, "(DMEUH|DMCOH)(.*[A-Z]+)([0-9]+)$", "$1$2/$3")

3 Likes

Hello @Heldyyyyy,

You can use the Java Snippet node and configure it with the following code:

List inputs = new ArrayList<>();
inputs.add(c_ol1);

String modifiedInputs = inputs.stream()
.map(s → {
if (s.startsWith(“DMCOH”) || s.startsWith(“DMEUH”)) {
return s.replaceAll(“(?<=\D)(?=\d)”, “/”);
} else {
return s;
}
})
.toArray(String::new);

out_new = modifiedInputs;

For your reference, I have attached images.

1 Like

Hi @takbb, this solution works perfectly! Thank you so much for your assistance. I’ve been puzzling over this for hours, wondering why it wasn’t capturing the formula. It turns out the issue was with the non-alpha characters. Thanks again! :sweat_smile:

Hi @tqAkshay95, I’ve also tried this and it also works and can be the alternative approach. I’m not very confident in using the Java snippet node since I’m not very familiar with Java coding and only use some other basic nodes :smile:. Nonetheless, thank you for your assistance!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.