Skip to content Skip to sidebar Skip to footer

Java - Javascript Regex Code Fails On 1.8 As Compared To 1.7 Using Javax.script.scriptengine;

I have a regex pattern in JavaScript which works fine in browser and in 1.7 environment of Java using javax.script.ScriptEngine; but it fails on 1.8. My code : ScriptEngineManager

Solution 1:

Your actual problem is shown by the output of the print:

executed regex result : null

So, the regex engine doesn't think your pattern matches the input.

Unsurprisingly null[2] doesn't work.

Two points about this:

  • You're calling trans_regex.exec(fileSrc) three times. Do it once and put the result in a variable.
  • Unless you can guarantee a match, you should check for a null result and handle it somehow.

I'd suggest adding a println to your Java to print the script and see exactly how the quoting has worked out. Then build up your regex anew from first principles, testing each iteration.

e.g. I truncated the regex down to nothing then rebuilt it as far as:

+"var trans_regex = /<span>(\\d{2}-\\d{2}-\\d{4})\\s*?(\\d{1,2}:\\d{2}\\s*?(?:am|pm))\\s*?/i;\n"

... and it doesn't error any more. Keep adding groups until it fails again. Either the reason will be clear, or you'll have a more focussed question for SO.

It's a bit of a monster regex, so it's not really reasonable to expect SO to debug it for you.

Post a Comment for "Java - Javascript Regex Code Fails On 1.8 As Compared To 1.7 Using Javax.script.scriptengine;"