将正则表达式转换为与 Java 兼容的正则表达式的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to convert regex to a java compatible regex?
提问by James
I have a regex defined in Python/Ruby/PHP that is like this
我有一个在 Python/Ruby/PHP 中定义的正则表达式,就像这样
"(forumdisplay.php\?.*page=%CURRENTPAGE%)"
When I do it for Java, I have to double escape that question mark to \\?
当我为 Java 做这件事时,我必须双重转义那个问号 \\?
Like so:
像这样:
"(forumdisplay.php\?.*page=%CURRENTPAGE%)";
Is there a function I can use to do that automatically? Or would I need to change all my regexes over to work with the Java regex engine?
是否有我可以使用的功能来自动执行此操作?或者我是否需要更改我所有的正则表达式以使用 Java 正则表达式引擎?
回答by bwawok
A good start is usually to just do a "find replace all" of "\" with "\\".
一个好的开始通常是用“\\”“查找替换所有”的“\”。
You aren't really doing a change to make this work with the java regex engine. You are just having to deal with the pains of storing a regex in a Java String... You could do this in a function, but that would make more code to maintain. I would suggest doing a find replace as described above...
您并没有真正进行更改以使其与 Java 正则表达式引擎一起工作。您只需要处理将正则表达式存储在 Java 字符串中的痛苦……您可以在函数中执行此操作,但这会需要维护更多代码。我建议按上述方法进行查找替换...
回答by Matthieu BROUILLARD
Personally I use within Eclipse the EXCELLENT plugins from the site http://www.bastian-bergerhoff.com/eclipse/features/
You will find there QuickREx for regular expression and also XPath developper plugin that I use a lot.
For QuickREx, just test you regular expression and press the button to copy it on your active editor with the good escapes characters.
It's a must, just give it a try.
我个人在 Eclipse 中使用来自网站http://www.bastian-bergerhoff.com/eclipse/features/的 EXCELLENT 插件,
您会在那里找到用于正则表达式的 QuickREx 以及我经常使用的 XPath 开发者插件。
对于 QuickREx,只需测试您的正则表达式,然后按下按钮将其复制到您的活动编辑器中,并使用好的转义字符。
这是必须的,试试吧。
回答by Greg Hewgill
Note that this is notthe Java regular expression engine that requires the double backslashes, but the Java compiler. When you write the following in Java source code:
请注意,这不是需要双反斜杠的 Java 正则表达式引擎,而是 Java编译器。当您在 Java 源代码中编写以下内容时:
"(forumdisplay.php\?.*page=%CURRENTPAGE%)"
the Java compiler interprets this as the string:
Java 编译器将其解释为字符串:
(forumdisplay.php\?.*page=%CURRENTPAGE%)
The Java regular expression engine then does exactly the same thing as other regular expression engines - the question mark (because it is escaped) is treated literally.
Java 正则表达式引擎然后做与其他正则表达式引擎完全相同的事情——问号(因为它被转义)按字面处理。
A similar thing happens in Python - the two strings below are identical:
Python 中也发生了类似的事情——下面的两个字符串是相同的:
r"(forumdisplay.php\?.*page=%CURRENTPAGE%)"
"(forumdisplay.php\?.*page=%CURRENTPAGE%)"
This is using the Python r
notation for a "raw" string where backslashes are not interpreted by the compiler.
这是r
对“原始”字符串使用 Python表示法,其中反斜杠不被编译器解释。
回答by Eric Chen
Try this online tool out https://www.regexplanet.com/advanced/java/index.html
试试这个在线工具https://www.regexplanet.com/advanced/java/index.html
It takes your normal regular expression and outputs the Java-compatible string expression. Saved me tons of time converting huge regex strings myself.
它采用您的普通正则表达式并输出与 Java 兼容的字符串表达式。为我自己转换巨大的正则表达式字符串节省了大量时间。
Note that not all regex expressions work in java. I've seen weird PHP validation regex that simply behaves differently in java pattern matching.
请注意,并非所有正则表达式都适用于 java。我见过奇怪的 PHP 验证正则表达式,它在 Java 模式匹配中的行为不同。