説明
この式は次を探します。
- アルファベット順に 4 つ以上の連続した文字 (例:
abcd
、tuvwxyz
)
- 同一の 4 文字以上または連続する文字 (例:
aaaa
, qqqqfdadas
)
^(?:(?!(?:abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)\1{3})).)*
Java コード例:
入力テキスト
abcd - not allowed
AbCd - not allowed
abc3 - allowed
abcr - allowed
PQRS - not allowed
pqrs - not allowed
pqra - allowed
aaaa - not allowed
qqqq - not allowed
aaab - allowed
qqqw - allowed
1234 - allowed
1111 - allowed
コード
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("^(?:(?!(?:abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)\\1{3})).)*",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
マッチ
$matches Array:
(
[0] => Array
(
[0] =>
[1] =>
[2] => abc3 - allowed
[3] => abcr - allowed
[4] =>
[5] =>
[6] => pqra - allowed
[7] =>
[8] =>
[9] => aaab - allowed
[10] => qqqw - allowed
[11] => 1234 - allowed
[12] => 1111 - allowed
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
)
)