3

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.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
seanEd
  • 889
  • 1
  • 12
  • 27

1 Answers1

7

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})+", "").

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • Nice. @Wiktor Any good tutorial to learn reg exp please? may with some advanced level tutorials explained as well? – Shreyas Sarvothama Oct 26 '16 at 15:55
  • Read [Lesson: Regular Expressions](https://docs.oracle.com/javase/tutorial/essential/regex/) first. Then, I can suggest doing all lessons at [regexone.com](http://regexone.com/), reading through [regular-expressions.info](http://www.regular-expressions.info), [regex SO tag description](http://stackoverflow.com/tags/regex/info) (with many other links to great online resources), and the community SO post called [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). Also, [rexegg.com](http://rexegg.com) is worth having a look at. – Wiktor Stribiżew Oct 26 '16 at 15:58
  • Thanks @WiktorStribiżew – seanEd Oct 26 '16 at 16:10
  • @seanEd Please consider accepting the answer. – Wiktor Stribiżew Jan 27 '21 at 00:38