Punctuation Regex in Java

前端 未结 2 1423
花落未央
花落未央 2020-11-29 09:50

First, i\'m read the documentation as follow

http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

And i want find any punctuation c

相关标签:
2条回答
  • 2020-11-29 10:05

    You may use character subtraction here:

    String pat = "[\\p{Punct}&&[^@',&]]";
    

    The whole pattern represents a character class, [...], that contains a \p{Punct} POSIX character class, the && intersection operator and [^...] negated character class.

    A Unicode modifier might be necessary if you plan to also match all Unicode punctuation:

    String pat = "(?U)[\\p{Punct}&&[^@',&]]";
                  ^^^^
    

    The pattern matches any punctuation (with \p{Punct}) except @, ', , and &.

    If you need to exclude more characters, add them to the negated character class. Just remember to always escape -, \, ^, [ and ] inside a Java regex character class/set. E.g. adding a backslash and - might look like "[\\p{Punct}&&[^@',&\\\\-]]" or "[\\p{Punct}&&[^@',&\\-\\\\]]".

    Java demo:

    String value = "#`~!#$%^,";
    String pattern = "(?U)[\\p{Punct}&&[^@',&]]";
    Pattern r = Pattern.compile(pattern);    // Create a Pattern object
    Matcher m = r.matcher(value);            // Now create matcher object.
    while (m.find()) {
        System.out.println("Found value: " + m.group());
    }
    

    Output:

    Found value: #
    Found value: !
    Found value: #
    Found value: %
    Found value: ,
    
    0 讨论(0)
  • 2020-11-29 10:11

    You're matching two characters, not one. Using a (negative) lookahead should solve the task:

    (?![@',&])\\p{Punct}
    
    0 讨论(0)
提交回复
热议问题