Sangli
Sangli

Reputation: 363

Java regex to replace all special characters in a String with an underscore also considering removing leading,trailing,multiple underscores

I would need a regular expression to replace all the special characters considering multiple with a single underscore and also not to add trailing and leading underscore if the String contains trailing and leading special characters, I have tried the following but it doesn't seem to work.

String myDefaultString = "_@##%Default__$*_123_"
myDefaultString.replaceAll("[\\p{Punct}&&[^_]]", "_")

My eventual result should be Default_123 where the regular expression needs to consider leading underscore and remove them keeping the underscore in between Default and 123 but also should remove trailing and multiple underscores in between the String.

Also tried the following regex

myDefaultString.replaceAll("[^a-zA-Z0-9_.]+", "_")

But does not seem to work, is what I'm trying to achieve very complicated or it there a better way to do it?

Upvotes: 1

Views: 2097

Answers (1)

anubhava
anubhava

Reputation: 785196

You may use this regex in replaceAll:

String str = "_@##%Default__$*_123_";
str = str.replaceAll("[\\p{Punct}&&[^_]]+|^_+|\\p{Punct}+(?=_|$)", "");
//=> "Default_123"

RegEx Demo

RegEx Details:

  • [\\p{Punct}&&[^_]]+: Match 1+ punctuation characters that are not _
  • |: OR
  • ^_+: Match 1+ underscores at start
  • |: OR
  • \\p{Punct}+(?=_|$): Match 1+ punctuation characters if that is followed by a _ or end of string.

Upvotes: 5

Related Questions