# Java：对2.5M字符串使用互斥的RegExs(15)的有效方法？

## 提问

• Regular expressions must check for the presence of:
• Punctuation
• Upper-case letters
• Lower-case letters
• Integers

``````U = Upper-case letter L = Lower-case letter P = Punctuation N = Number

---- null
U--- [A-Z]+
UL-- [A-Za-z]+
U-N- [A-Z0-9]+
ULN- [A-Za-z0-9]+
ULNP [\\p{Punct}\\sA-Za-z0-9]+
-L-- [a-z]+
-LN- [a-z0-9]+
--N- [0-9]
---P [\\p{Punct}\\s]+
U--P [\\p{Punct}\\sA-Z]+
-L-P [\\p{Punct}\\sa-z]+
--NP [\\p{Punct}\\s0-9]+
UL-P [\\p{Punct}\\sA-Za-z]+
U-NP [\\p{Punct}\\sA-Z0-9]+
ULNP [\\p{Punct}\\sA-Za-z0-9]+
``````

``````public static void main(String[] args) {

try {
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String s = scan.nextLine();
/*****************************************
* Evaluate password Strings using RegExs
******************************************/
if(s.matches("[A-Z0-9]+")){
//Upper-case & numeric

} else if(s.matches("[a-z0-9]+")){
//Lower-case & numeric

} else if(s.matches("[A-Za-z0-9]+")){
//Alphanumeric

} else if(s.matches("[A-Za-z]+")){
//Upper-case & lower-case

} else if(s.matches("[0-9]+")){
//Numeric

} else if(s.matches("[A-Z]+")){
//Upper-case

}  else if(s.matches("[a-z]+")){
//Lower-case

} else if(s.matches("[\\p{Punct}\\s]+")){
//Punctuation

} else if(s.matches("[\\p{Punct}\\sA-Z]+")){
//Punctuation & upper-case

} else if(s.matches("[\\p{Punct}\\sa-z]+")){
//Punctuation & lower-case

} else if(s.matches("[\\p{Punct}\\s0-9]+")){
//Punctuation & numeric

} else if(s.matches("[\\p{Punct}\\sA-Za-z]+")){
//Punctuation & alphabetical

} else if(s.matches("[\\p{Punct}\\sA-Z0-9]+")){
//Punctuation & upper-case & numeric

} else if(s.matches("[\\p{Punct}\\sa-z0-9]+")){
//Punctuation & lower-case & numeric

} else if(s.matches("[\\p{Punct}\\sA-Za-z0-9]+")){
//Punctuation & alphanumeric

} else {
System.err.println("ERROR: unhandled RegEx");
}
} //loop
} catch (FileNotFoundException fnfe){
System.err.println(fnfe.getMessage());
}

}//main()
``````

(main()的底部)

``````public static void main(String[] args) {
Analyzer a = new Analyzer(); //used by Java reflections object

try {
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String s = scan.nextLine();
//Flags
boolean U_flag = false;
boolean L_flag = false;
boolean N_flag = false;
boolean P_flag = false;

for(int i=0; i<s.length(); i++){
String c = s.substring(i, i);
/*****************************************
* Set flags (U,L,N,P)
****************************************/
//U_flag (upper-case)
if(c.matches("[A-Z]+")){
U_flag = true;
}
//L_flag (lower-case)
if(c.matches("[a-z]+")){
L_flag = true;
}
//N_flag (numeric)
if(c.matches("[0-9]+")){
N_flag = true;
}
//P_flag (punctuation)
if(c.matches("[\\p{Punct}\\s]+")){
P_flag = true;
}
/*****************************************
* Identify corresponding counter variable
****************************************/
String dest = "";

//U_flag
if(U_flag){dest.concat("U");
} else {dest.concat("_");}

//L_flag
if(L_flag){dest.concat("L");
} else {dest.concat("_");}

//N_flag
if(N_flag){dest.concat("N");
} else {dest.concat("_");}

//P_flag
if(P_flag){dest.concat("P");}

//increment variable stored in dest (Java reflections?)

}//for-loop
} //while-loop
} catch (FileNotFoundException fnfe){
System.err.println(fnfe.getMessage());
}

}//main()
``````

## 最佳答案

``````U--- [A-Z]+
UL-- [A-Za-z]+
U-N- [A-Z0-9]+
ULN- [A-Za-z0-9]+
ULNP [\\p{Punct}\\sA-Za-z0-9]+
``````

``````for (String s in allStrings)
{
int charClass = 0
for (Char c in s.characters)
{
case c
when upper-case:
charClas |= 8
break;
when lower-case:
charClas |= 4
break;
when numeric:
charClas |= 2
break;
when punctuation:
charClas |= 1
break;
}
// do something with charClass
}
``````

``````String[] ulnpStrings = { "----","---P","--N-","--NP","-L--", "-L-P",... etc };
``````

``````int[] ulnpCounts = new int[16];
``````

``````    ...
// do something with charClass
unlpCounts[charClass]++
}
for (int i=0; i<unlpStrings.length; i++)
{
System.out.printf("%s %6d\n",unlpStrings[i],unlpCounts[i]);
}
``````