简体   繁体   English

将php正则表达式转换为java正则表达式

[英]convert php regex to java regex

Can somebody help me with converting a php regex to java regex?有人可以帮我将 php regex 转换为 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 )

How I understand preg_replace function in php is the same as replaceAll in java?.. So in java code it would be like this.我如何理解php中的preg_replace函数与java中的replaceAll相同?..所以在java代码中它会是这样的。

str = str.replaceAll("{(.)\1+}", "$1");
str = str.replaceAll("{[ \'-_\(\)]}", "");

But this code wont be work because how I know that regex in php is different by java.但是这段代码将不起作用,因为我如何知道 php 中的正则表达式与 java 不同。

Please, somebody help me!请有人帮助我! Thanks a lot))非常感谢))

update更新

final result is最终结果是

str = str.replaceAll("(.)\\1+", "$1");
str = str.replaceAll("[ '-_()]", "");

The only difference with Java regex is that you have to escape the backslashes.与 Java 正则表达式的唯一区别是您必须转义反斜杠。

str = str.replaceAll("(.)\\1+", "replacerString");
str = str.replaceAll("[ \\'-_\\(\\)]", "");

For this PHP regex:对于这个 PHP 正则表达式:

$str = preg_replace ( '{(.)\1+}', '$1', $str );
$str = preg_replace ( '{[ \'-_\(\)]}', '', $str )

In Java:在 Java 中:

str = str.replaceAll("(.)\\1+", "$1");
str = str.replaceAll("[ '-_\\(\\)]", "");

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 中完成的更好答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM