How to below java code in knime

// Assuming your column is named “string” and the result should be a new column named “Result”

// Import necessary classes
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// Define the regular expression pattern
Pattern pattern = Pattern.compile(“pat: (\w+)”);

// Loop through each row in the input table
for (int i = 0; i < string.length; i++) {
// Get the value from the “string” column
String inputString = string[i];

// Create a matcher object
Matcher matcher = pattern.matcher(inputString);

// Check if the pattern is found
if (matcher.find()) {
    // Get the matched value
    String matchedValue = matcher.group(1);

    // Determine the result based on the matched value
    String result;
    if (matchedValue.equals("12#₹&4")) {
        result = "TP";
    } else if (matchedValue.equals("hop")) {
        result = "CHECK";
    } else {
        result = "UNKNOWN"; // You can adjust this based on your needs
    }

    // Set the result in the output column
    Result[i] = result;
} else {
    Result[i] = "NO_MATCH"; // If the pattern is not found
}

}

Hi @Ashok121

Using java snippet, you can remove all the loop part to iterate the rows, since java snippet does this for you. It gets invoked for each row, so within one invocation, there is no concept of different rows.

so your code will end up something like this:

I’ve uploaded an example, although in my test data, the 12#₹&4 does not get captured as a single word, so you may need some adjustments, or it may be that it doesn’t work with my localisation. It could also be that it lost something during the upload on the forum, as you hadn’t marked it as “preformatted text”.
image

You can see I added an extra output column to show the value of the matchedvalue which will help with debugging.

image

Hopefully it gives you something to work with :slight_smile:

KNIME_java demo.knwf (7.7 KB)

You can adjust the names of your input and output columns by overtyping them in the lower tables.