Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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 )

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("{[ \'-_\(\)]}", "");

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))

update

final result is

str = str.replaceAll("(.)\\1+", "$1");
str = str.replaceAll("[ '-_()]", "");
share|improve this question
1  
Don't escape more than you need to. Your second pattern could just as well be [ '-_()] (although in PHP you'd still have to escape the ', of course). –  Martin Büttner Jun 26 '13 at 18:42
    
If you're not using any non-core regex features, regexes are the same everywhere and the only thing to look out for is stuff like whether you need to double up (escape) backslashes or not, multi-line behaviour, etc –  Patashu Jun 27 '13 at 0:53

2 Answers 2

up vote 1 down vote accepted

For this PHP regex:

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

In 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.

share|improve this answer
    
Thanks!)) Yep! Surely I add the example of input string when I would be know it. At this time I don't know what is it) –  falcon Jun 26 '13 at 20:17

The only difference with Java regex is that you have to escape the backslashes.

str = str.replaceAll("(.)\\1+", "replacerString");
str = str.replaceAll("[ \\'-_\\(\\)]", "");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.