有没有办法摆脱字符串前端和后端的符号?
例如,
"hello," -> "hello"
"hello;" -> "hello"
换句话说,删除单词之后,之前或之内的所有标点符号,除了单引号和单个短划线,如果它们后跟更多字母。
更多例子,
"lies,", "'This", "all-eating" and "deserv'd."
将成为
"lies", "this", "all-eating" and "deserv'd"
答案 0 :(得分:0)
使用posix正则表达式术语\p{Punct}
:
str = str.replaceAll("^\\p{Punct}*|\\p{Punct}+$|\\p{Punct}{2,}", "")
使用"两个或更多&#34>删除中间字符标点符号。匹配。
一些测试代码:
for (String str : new String[]{"hello,", "hello;", "li--es", "'This", "all-eating", "deserv'd."})
System.out.println(str.replaceAll("^\\p{Punct}*|\\p{Punct}+$|\\p{Punct}{2,}", ""));
输出:
hello
hello
lies
This
all-eating
deserv'd