Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java正则表达式-必需的字符串验证_Java_Regex - Fatal编程技术网

java正则表达式-必需的字符串验证

java正则表达式-必需的字符串验证,java,regex,Java,Regex,我需要一个正则表达式,它检查字符串,如果所有条件都满足,则返回true 条件: 字符串长度8字符 字符串包含2小写字母、4大写字母和2符号(,-) 所有符号或字符可以是字符串中的任何位置 我的Junit测试用例 package com.ap; import static org.junit.Assert.assertTrue; import org.junit.Test; public class RegularExpressionTest { private static

我需要一个正则表达式,它检查字符串,如果所有条件都满足,则返回true

条件:

  • 字符串长度8字符

  • 字符串包含2小写字母、4大写字母和2符号(
    -

  • 所有符号或字符可以是字符串中的任何位置

  • 我的Junit测试用例

    package com.ap;
    
    import static org.junit.Assert.assertTrue;
    import org.junit.Test;
    
        public class RegularExpressionTest {
    
        private static RegularExpression re = new RegularExpression();
    
        @Test
        public void correctInput1() {
            boolean result = re.check("abABCD#-");  
            assertTrue(result);     
        }
    
        @Test
        public void correctInput2() {
            boolean result = re.check("bABCD#-a");  
            assertTrue(result);     
        }
    
        @Test
        public void correctInput3() {
            boolean result = re.check("bABCD#-a");  
            assertTrue(result);     
        }
    
        @Test
        public void correctInput4() {
            boolean result = re.check("ABCD#-ab");  
            assertTrue(result);     
        }
    
        @Test
        public void correctInput5() {
            boolean result = re.check("CD#-abAB");  
            assertTrue(result);     
        }
    
        @Test
        public void correctInput6() {
            boolean result = re.check("#aABb-CD");  
            assertTrue(result);     
        }
    
        @Test
        public void correctInput7() {
            boolean result = re.check("-A#bDaBC");  
            assertTrue(result);     
        }
    
        @Test
        public void incorrectInput1() {
            boolean result = re.check("abABC#-");   //total 7 character
            assertTrue(result);     
        }
        @Test
        public void incorrectInput2() {
            boolean result = re.check("abABCDE#-"); //total 9 character
            assertTrue(result);     
        }
    
        @Test
        public void incorrectInput3() {
            boolean result = re.check("abABCDE#");  // "-" symbol absent 
            assertTrue(result);     
        }
    
        @Test
        public void incorrectInput4() {
            boolean result = re.check("abABCDEFG"); // Symbol absent
            assertTrue(result);     
        }
    
        @Test
        public void incorrectInput5() {
            boolean result = re.check("ABCDEEF#-"); //lower case letter absent
            assertTrue(result);     
        }
    
        @Test
        public void incorrectInput6() {
            boolean result = re.check("abcdef#-");  // Upper case letter absent
            assertTrue(result);     
        }
    
        @Test
        public void incorrectInput7() {
            boolean result = re.check("abcABCf#-");//4 Uppercase & 2 lowercase character
            assertTrue(result);     
        }
    
    
    }
    


    总体思路:

    • (?:.*x){n}
      -如果字符串至少有nxes,则匹配
    • (?=…)
      -可以连接多个以检查字符串的多个属性
    • ^.{n}$
      -从字符串的开头到字符串的结尾,正好有n个字符

    到目前为止,您尝试过什么正则表达式?
    (?=(?:.*[a-z]){2})(?=(?:.*[A-Z]){4})(?=(?:.*[-#]){2})^.{8}$