Can I define custom character class shorthands?

耗尽温柔 提交于 2019-12-23 07:39:08

问题


Java provides some useful character classes like \d and \w. Can I define my own character classes? For example, it would be useful to be able to define shorthands for character classes like [A-Za-z_].


回答1:


Can I define my own character classes?

No, you can't.

Personally, when I have a (slightly) complicated regex, I break the regex up in smaller sub-regexes and then "glue" them together with a String.format(...) like this:

public static boolean isValidIP4(String address) {
    String block_0_255 = "(0|[1-9]\\d|2[0-4]\\d|25[0-5])";
    String regex = String.format(
            "%s(\\.%s){3}", 
            block_0_255, block_0_255
    );
    return address.matches(regex);
}

which is far more readable than a single pattern:

"(0|[1-9]\\d|2[0-4]\\d|25[0-5])(\\.(0|[1-9]\\d|2[0-4]\\d|25[0-5])){3}"

Note that this is just a quick example: validating IP addresses can probably better be done by a class from the java.net package, and if you'd do it like that, the pattern should be placed outside the method and pre-compiled.

Be careful with % signs inside your pattern!




回答2:


I suspect the closest you can get assuming you don't want to write your own complete regex engine is to write a string preprocessor that converts a string containing your shorthand symbols into another string with the symbols expanded.




回答3:


Yes and no. Yes, because [] is already a character class. No, you can not define custom "shortcuts" like \w. One reason is, that it were never JAVAs decision. These character classes are standardized somewhere else (POSIX?).

My 2 cent: Regular expressions are complex enough be itself. You shouldn't confuse yourself and others by introducing even more.




回答4:


I assume you're talking about using these custom shortcuts in regular expressions? If so, the answer is no, unless you write your own regular expression parser (which is something you don't want to do). Those shortcuts are part of a predefined spec that can't be altered.



来源:https://stackoverflow.com/questions/6775383/can-i-define-custom-character-class-shorthands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!