[英]Need help to match specific characters with a Java regex
I need to match characters that do not belong to the following character set : 我需要匹配不属于以下字符集的字符:
abcdefghijklmnopqrstu vwxyz ABCDEFGHIJKLMNOPQRSTU VWXYZ 0 1 2 3 4 5 6 7 8 9 / - ? abcdefghijklmnopqrstu vwxyz ABCDEFGHIJKLMNOPQRSTU VWXYZ 0 1 2 3 4 5 6 7 8 9 / - ? : ( ) . :()。 , ' + space ,'+空间
To do that, I'm using this regex : 要做到这一点,我正在使用这个正则表达式:
String regex = "[^\\da-zA-Z/\\-\\?:\\(\\)\\.\\,'\\+ ]+";
Unfortunatly, that does not work. 不幸的是,这不起作用。
I tried this too (negation): 我也试过了(否定):
String regex = "(?![\\da-zA-Z/\\-\\?:\\(\\)\\.\\,'\\+ ]+)";
But it's not ok. 但它并不好。
Anyone can help ? 有人可以帮忙吗?
I don't think you can use a predefined character class like \\d
inside another character class. 我认为你不能在另一个角色类中使用像\\d
这样的预定义角色类。 Also, most of the characters you're escaping aren't special within a character class (although the escaping should be harmless). 此外,您逃离的大多数角色在角色类中并不特殊(尽管逃逸应该是无害的)。 So: 所以:
String regex = "[^0-9a-zA-Z/\\-?:().,'+ ]+";
Side note: In your question, you said you wanted to replace '
(a fancy curly apostrophe), but in your regex you have just a normal apostrophe '
. 旁注:在你的问题中,你说你想要替换'
(一个奇特的撇号撇号),但在你的正则表达式中你只有一个正常的撇号'
。 So change that if needed. 所以如果需要改变它。
Here's a test: 这是一个测试:
public class RegTest {
public static final void main(String[] args) {
String regex, test, result;
// First, test without the negation and make sure it *does* replace the target chars
regex = "[0-9a-zA-Z/\\-?:().,'+ ]+";
test = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-?:().,'+";
result = test.replaceAll(regex, "%");
System.out.println(result);
// Prints %
// Now, test *with* the negation and make sure it matches other characters (I put
// a few at the beginning) but not those
regex = "[^0-9a-zA-Z/\\-?:().,'+ ]+";
test = "[@!\"~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-?:().,'+";
result = test.replaceAll(regex, "%");
System.out.println(result);
// Prints %abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-?:().,'+
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.