Convert Javascript regular expression to Java syntax

*爱你&永不变心* 提交于 2019-11-27 10:55:36

问题


I am aware that regEx are common across languages...But I am having trouble in writing the Java syntax. I have a regular expression coded in JS as;

if((/[a-zA-Z]/).test(str) && (/[0-9]|[\x21-\x2F|\x3A-\x40|\x5B-\x60|\x7B-\x7E]/).test(str))         
return true;

How do I write the same in Java ?

I have imported

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Just to add, from what I am trying it is saying \x is an invalid escape character..


回答1:


Change the leading and trailing '/' characters to '"', and then replace each '\' with "\\".

Unlike, Javascript, Perl and other scripting languages, Java doesn't have a special syntax for regexes. Instead, they are (typically) expressed using Java string literals. But '\' is the escape character in a Java string literal, so each '\' in the original regex has to be escaped with a 2nd '\'. (And if you have a literal backslash character in the regex, you end up with "\\\\" in the Java string literal!!)

This is a bit confusing / daunting for Java novices ... but it is totally logical. Just remember that you are using a Java string literal to express the regex.


However as @antak notes, there are various differences between regex languages in Java and Javascript. So if you take an arbitrary Javascript regex and transliterate it to Java as above, it might not work.

Here are some references that summarize the differences.

  • https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines
  • https://gist.github.com/CMCDragonkai/6c933f4a7d713ef712145c5eb94a1816



回答2:


The only thing you have to do is to duplicate back slashes.

Pattern p1 = Pattern.compile("[a-zA-Z]");
Pattern p2 = Pattern.compile("[0-9]|[\\x21-\\x2F|\\x3A-\\x40|\\x5B-\\x60|\\x7B-\\x7E]");

if (p1.matcher(str).find() && p2.matcher(str).find()) {
    return true;
}



回答3:


If you really need Javascript regex semantics in Java, one approach would be to use the embedded Javascript engine to evaluate the regex. For example:

javax.script.ScriptEngineManager se = new javax.script.ScriptEngineManager();
javax.script.ScriptEngine engine = se.getEngineByName("js");

String regExp = "/^\\d+$/";
engine.put("str", "1234");
engine.eval("var rgx=" + regExp);
Object value = engine.eval(
    "function validate(r, s){ return (r).test(s);};validate(rgx, str);");
logger.log(value);



回答4:


You can use online regex evaluators like https://regex101.com for conversion.

  1. Go to https://regex101.com
  2. Choose ECMAScript (JavaScript) FLAVOR
  3. Insert your regex
  4. Open TOOLS -> Code Generator (LANGUAGE - Java)
  5. Copy-paste

Even though it isn't hardcore programmer way, it is significantly less error-prone. Especially if you need to convert only one or two expressions.




回答5:


Java regular expressions are first and foremost strings, so you must start with double quotes and not /. Also, in java, you need to escape the \ by doing two of them like so \\.

Take a look at this tutorial from Oracle for more information.




回答6:


If you want to use the same regex in Javascript as well as Java try to get the regex string at runtime, rather than trying to define the regex at compile time. At compile time it will check for syntax and it will give you invalid escape character error, however at runtime it will not check for the syntax and will directly compile the pattern.

If you can get the regex from API or can read it from locally stored text file, it will great.



来源:https://stackoverflow.com/questions/8754444/convert-javascript-regular-expression-to-java-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!