문제

I have some text files that contain multiple punctuation marks, so I need to reduce those to single punctuation marks.

Here is some sample text:

They are working in London..... he is a Java developer !!!!! they are playing------ She is working_______

This is the required output:

They are working in London.he is a Java developer !they are playing- She is working_

I need some help with the Java regex.

Thanks

도움이 되었습니까?

해결책

Use backreference (\1+) to match repeated character.

Try following:

String text = "They are working in London..... he is a Java developer !!!!! they are playing------ ---- ---- She is working_______";
String replaced = text.replaceAll("(?:([-.!_])\\1+\\s*)+", "$1");
System.out.println(replaced);

prints

They are working in London.he is a Java developer !they are playing-She is working_

다른 팁

You can try this

   String str = "They are working in London..... he is a Java developer !!!!! they are playing-----She is working_______";
   String newStr = str.replaceAll("([|\\-|\\.|\\!|\\_])\\1+", "$1");
   System.out.println(newStr);

Live Demo

Out put

They are working in London. he is a Java developer ! they are playing-She is working_

Try something like this:

/([.;,?!-_]){2,}/$1/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top