Removing all punctuation except - and _ from a java string using RegEx

社会主义新天地 提交于 2019-12-20 03:28:08

问题


I am trying to replace all punctuation except the - and _ using a method I found here, but I can only get it to work on " using the exact code as posted which used a negative lookahead:

(?!")\\p{punct}

//Java example:

String string = ".\"'";
System.out.println(string.replaceAll("(?!\")\\p{Punct}", ""));

I tried:

name = name.replaceAll("(?!_-)\\p{Punct}", ""); // which just replaces all punctuation.

name = name.replaceAll("(?!\_-)\\p{Punct}", ""); // which gives an error.

Thanks.


回答1:


Use a character class subtraction (and add a + quantifier to match chunks of 1 or more punctuation chars):

name = name.replaceAll("[\\p{Punct}&&[^_-]]+", "");

See the Java demo.

The [\\p{Punct}&&[^_-]]+ means match any char from \p{Punct} class except _ and -.

The construction you found can also be used, but you'd need to put the - and _ into a character class, and use .replaceAll("(?![_-])\\p{Punct}", ""), or .replaceAll("(?:(?![_-])\\p{Punct})+", "").



来源:https://stackoverflow.com/questions/40266711/removing-all-punctuation-except-and-from-a-java-string-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!