繁体   English   中英

需要帮助将特定字符与Java正则表达式匹配

[英]Need help to match specific characters with a Java regex

我需要匹配属于以下字符集的字符:

abcdefghijklmnopqrstu vwxyz ABCDEFGHIJKLMNOPQRSTU VWXYZ 0 1 2 3 4 5 6 7 8 9 / - ? :()。 ,'+空间

要做到这一点,我正在使用这个正则表达式:

String regex = "[^\\da-zA-Z/\\-\\?:\\(\\)\\.\\,'\\+ ]+";

不幸的是,这不起作用。

我也试过了(否定):

String regex = "(?![\\da-zA-Z/\\-\\?:\\(\\)\\.\\,'\\+ ]+)";

但它并不好。

有人可以帮忙吗?

我认为你不能另一个角色类中使用像\\d这样的预定义角色类。 此外,您逃离的大多数角色在角色类中并不特殊(尽管逃逸应该是无害的)。 所以:

String regex = "[^0-9a-zA-Z/\\-?:().,'+ ]+";

旁注:在你的问题中,你说你想要替换' (一个奇特的撇号撇号),但在你的正则表达式中你只有一个正常的撇号' 所以如果需要改变它。

这是一个测试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM