java正则表达式-必需的字符串验证
我需要一个正则表达式,它检查字符串,如果所有条件都满足,则返回true 条件: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
,-
)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);
}
}
总体思路:
-如果字符串至少有nxes,则匹配(?:.*x){n}
-可以连接多个以检查字符串的多个属性(?=…)
-从字符串的开头到字符串的结尾,正好有n个字符^.{n}$
(?=(?:.*[a-z]){2})(?=(?:.*[A-Z]){4})(?=(?:.*[-#]){2})^.{8}$