将php正则表达式转换为java正则表达式 [英] convert php regex to java regex
问题描述
有人可以帮我将 php regex 转换为 java regex 吗?
Can somebody help me with converting a php regex to java regex?
如果您能帮助我,那就太好了,我将不胜感激,因为我在正则表达式方面不是很擅长.
It would be great and I would be appreciate you if you can help me, because I'm not so strong in regex.
$str = preg_replace ( '{(.)\1+}', '$1', $str )
$str = preg_replace ( '{[ \'-_\(\)]}', '', $str )
我如何理解 php 中的 preg_replace
函数与 java 中的 replaceAll
相同?..所以在java代码中它会是这样的.
How I understand preg_replace
function in php is the same as replaceAll
in java?..
So in java code it would be like this.
str = str.replaceAll("{(.)\1+}", "$1");
str = str.replaceAll("{[ \'-_\(\)]}", "");
但是这段代码不起作用,因为我怎么知道 php 中的正则表达式与 java 不同.
But this code wont be work because how I know that regex in php is different by java.
拜托,有人帮帮我!非常感谢))
Please, somebody help me! Thanks a lot))
更新
最终结果是
str = str.replaceAll("(.)\\1+", "$1");
str = str.replaceAll("[ '-_()]", "");
推荐答案
对于这个 PHP 正则表达式:
For this PHP regex:
$str = preg_replace ( '{(.)\1+}', '$1', $str );
$str = preg_replace ( '{[ \'-_\(\)]}', '', $str )
在 Java 中:
str = str.replaceAll("(.)\\1+", "$1");
str = str.replaceAll("[ '-_\\(\\)]", "");
我建议您提供您的输入和预期输出,然后您将获得有关如何在 PHP 和/或 Java 中完成的更好答案.
I suggest you to provide your input and expected output then you will get better answers on how it can be done in PHP and/or Java.
这篇关于将php正则表达式转换为java正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!