Java 删除非字母数字字符之间的空白
如何删除非字母数字字符之间的空白?比如说 到Java 删除非字母数字字符之间的空白,java,regex,Java,Regex,如何删除非字母数字字符之间的空白?比如说 到 您可以使用此基于查找的正则表达式进行搜索: (?<![\p{L}\p{N}]) +| +(?![\p{L}\p{N}]) (? 并用空字符串替换它 在Java中: String repl = input.replaceAll( "(?<![\\p{L}\\p{N}]) +| +(?![\\p{L}\\p{N}])", "" ); String repl=input.replaceAll(“(?请像这样尝试正则表达式: public
您可以使用此基于查找的正则表达式进行搜索:
(?<![\p{L}\p{N}]) +| +(?![\p{L}\p{N}])
(?
并用空字符串替换它
在Java中:
String repl = input.replaceAll( "(?<![\\p{L}\\p{N}]) +| +(?![\\p{L}\\p{N}])", "" );
String repl=input.replaceAll(“(?请像这样尝试正则表达式:
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, ""));
}
我们是否也应该按字母顺序加入dot
?
String repl = input.replaceAll( "(?<![\\p{L}\\p{N}]) +| +(?![\\p{L}\\p{N}])", "" );
(?<=\W)[ ]+|[ ]+(?=\W)
(?<=\\W)[ ]+|[ ]+(?=\\W)
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, ""));
}
anti-C6/36 membrane antibodies
D2 NS1-P1-specific antibodies