如何为接受的字符串制作正则表达式
示例: - 正则表达式不应接受以下内容
"""
"\"
"\\\"
正则表达式应该接受以下内容
"\""
"\r"
"\ " here the single backslash is having space after it so it is should be accepted.
"\\"
"\\\cloud"
"\\\ "
"clo$d"
"cloud space"
使用Java和REGEX的反斜杠,它在Java中看起来确实很难看:
static String REGEX = "([^\"\\\\]|\\\\[^\\\\]+|\\\\\\\\)+";
public static void main(String[] args) {
test("\"");
test("\\");
test("\\\\\\");
System.out.println();
test("\\\"");
test("\\r");
test("\\ ");
test("\\\\\\cloud");
test("\\\\\\ ");
test("clo$d");
test("cloud space");
}
public static void test(String s) {
System.out.println(s + ": " + REGEX + " " + Pattern.matches(REGEX, s));
}