Java node regex

I am new to Java Scripting. Can anybody help with regular expression in Java Snippet node please. Attached is the example that i am trying to work.

Thanks,

The regular expressions are not in Java syntax currently. In Java, there is nothing like \regularExpression\, you either write "regularExpression", or use Pattern.compile for compiling the expression once.

Best,
Philipp

I think you need to change your code slightly to:

String re = "(\w+)\s(\w+)";
String str = "John Smith";

String ab="";
if (str.matches(re)){
ab="T";
} else {
ab=F;

return ab;

You might need to escape your escape characters too, so the first line becomes:

String re = "(\\w+)\\s(\\w+)";

You could also abbreviate to:

String ab = (str.matches("(\w+)\s(\w+)")) ? "T" : "F";
return ab;

 

(Again with the possible need to escape the escapes)

Otherwise, you need to use Pattern.compile - google will find you plenty of examples, but for this sort of simple case, this is the easiest solution.

Steve

This is exactly what i was looking for. I appreciate it.

Thank you

 

Steve,

Neat code example, thanks! Much appreciated by non-coders like me.

- E