如何删除非字母数字字符之间的空白?例如
抗C6 / 36膜抗体D2 NS1 - P1特异性抗体
至
抗C6/36膜抗体D2 NS1-P1特异性抗体
发布于 2015-02-17 15:34:06
(?<=\W)[ ]+|[ ]+(?=\W)尝试this.Replace by empty string.See演示。
https://regex101.com/r/zB3hI5/11
对于java,它将是
(?<=\\W)[ ]+|[ ]+(?=\\W)发布于 2015-02-17 15:33:11
您可以使用此基于查找的regex进行搜索:
(?<![\p{L}\p{N}]) +| +(?![\p{L}\p{N}])用空字符串代替。
In Java:
String repl = input.replaceAll( "(?<![\\p{L}\\p{N}]) +| +(?![\\p{L}\\p{N}])", "" );如果空格后面没有字母数字,或者前面没有字母数字,(?<![\p{L}\p{N}]) | (?![\p{L}\p{N}])的意思是匹配一个空格。
发布于 2015-02-17 15:39:08
试试像这样的regex:
public static void main(String[] args) {
String s1 = "anti - C6 / 36 membrane antibodies";
String s2 = "D2 NS1 - P1 - specific antibodies";
String pattern = "\\s+(?=[^a-zA-Z0-9]+)|(?<=[^a-zA-Z0-9])\\s+";// replace all spaces either preceeded by or followed by a non-alphanumeric character
System.out.println(s1.replaceAll(pattern, ""));
System.out.println(s2.replaceAll(pattern, ""));
}O/P:
anti-C6/36 membrane antibodies
D2 NS1-P1-specific antibodieshttps://stackoverflow.com/questions/28565017
复制相似问题