简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM