简体   繁体   English

Java正则表达式用下划线替换字符串中的所有特殊字符,同时考虑删除前导、尾随、多个下划线

[英]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.我的最终结果应该是Default_123 ,其中正则表达式需要考虑前导下划线并删除它们,将下划线保留在Default123之间,但也应该删除字符串之间的尾随和多个下划线。

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 :您可以在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 _ [\\\\p{Punct}&&[^_]]+ : 匹配 1+ 个不是_标点字符
  • | : OR : 或者
  • ^_+ : Match 1+ underscores at start ^_+ : 在开始时匹配 1+ 个下划线
  • | : OR : 或者
  • \\\\p{Punct}+(?=_|$) : Match 1+ punctuation characters if that is followed by a _ or end of string. \\\\p{Punct}+(?=_|$) :匹配 1+ 个标点字符,如果后面跟有_或字符串结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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