有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    您可以使用charclass并集:

    String myCharclass = "[a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF \\-&*()_+|~=`{}\\[\\]:\";'<>?,.]"
    
    if (condition)
        myCharclass = "[/" + charclass + "]"; // a character class union
    
    String regex = "^" + myCharclass + "{0,900}$";
    

    如果使用almson-regex编写,它将如下所示:

    import static net.almson.util.Regex.*;
    
    String myCharclass = charclassUnion (LETTER, DIGIT, charclassRange ('\u00A0', '\uD7FF'), charclassRange ('\uF900', '\uFDCF'), charclassRange ('\uFDF0','\uFFEF'), charclass (' ', '-', '&', '*', '(', ')', '_', '+', '|', '~', '=', '`', '{', '}', '[', ']', ':', '"', ';', '\'', '<', '>', '?', ',', '.'));
    
    if (condition)
        myCharclass = charclassUnion (myCharclass, charclass ('/'));
    
    regex = START_BOUNDARY + between (0, 900, myCharclass) + END_BOUNDARY;
    
  2. # 2 楼答案

    最好的方法可能是分别定义它们,然后根据您的if条件使用它们

        String regex1 = "^[a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF \\-&*()_+|~=`{}\\[\\]:\";'<>?,.]{0,900}$";
        String regex2 = "^[a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF \\-&*()_+|~=`{}\\[\\]:\";'<>?,/.]{0,900}$";
    
        String currentRegex;
        if (condition is true) { 
              currentRegex = regex2;
        } else {
            currentRegex = regex1;
        }