Java中的标点正则表达式 [英] Punctuation Regex in Java
本文介绍了Java中的标点正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我阅读文档如下
http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
我希望找到任何标点符号,除了@',&但我不太明白。
And i want find any punctuation character EXCEPT @',& but i don't quite understand.
这是:
public static void main( String[] args )
{
// String to be scanned to find the pattern.
String value = "#`~!#$%^";
String pattern = "\\p{Punct}[^@',&]";
// Create a Pattern object
Pattern r = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
// Now create matcher object.
Matcher m = r.matcher(value);
if (m.find()) {
System.out.println("Found value: " + m.groupCount());
} else {
System.out.println("NO MATCH");
}
}
结果为否比赛。
是否有任何不匹配?
Result is NO MATCH.
Is there any mismatch ?
谢谢
MRizq
Thanks
MRizq
推荐答案
你匹配两个字符,而不是一个。使用(负面)预测应该可以解决任务:
You're matching two characters, not one. Using a (negative) lookahead should solve the task:
(?![@',&])\\p{Punct}
这篇关于Java中的标点正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文