正则表达式[关闭]

问题描述 投票:-1回答:1

如何为接受的字符串制作正则表达式

  1. 双引号,仅当它遵循反斜杠时
  2. 如果有反奇数的奇数,那么最后一个反斜杠应该包含特殊字符或任何文字或任何字母

示例: - 正则表达式不应接受以下内容

"""
"\"
"\\\"

正则表达式应该接受以下内容

"\""
"\r"
"\ "  here the single backslash is having space after it so it is should be accepted.
"\\"
"\\\cloud"
"\\\ "
"clo$d"
"cloud space" 
java regex
1个回答
0
投票

使用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));
}
© www.soinside.com 2019 - 2024. All rights reserved.