问题
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