ネガティブ先読み^(?!\.).+$
は機能します。これはJavaです:
String[] files = {
".afile",
".anotherfile",
"bfile.file",
"bnotherfile.file",
".afolder/",
".anotherfolder/",
"bfolder/",
"bnotherfolder/",
"",
};
for (String file : files) {
System.out.printf("%-18s %6b%6b%n", file,
file.matches("^(?!\\.).+$"),
!file.startsWith(".")
);
}
出力は次のとおりです(ideone.comで見られるように):
.afile false false
.anotherfile false false
bfile.file true true
bnotherfile.file true true
.afolder/ false false
.anotherfolder/ false false
bfolder/ true true
bnotherfolder/ true true
false true
非正規表現の使用にも注意してくださいString.startsWith
。とにかく正規表現は必要ないので、おそらくこれが最良で最も読みやすいソリューションであり、正規表現(少なくともJavaでは)がどこにあるのかということstartsWith
です。O(1)
O(N)
空白の文字列の不一致に注意してください。これが可能な入力であり、これを返したい場合は、次のfalse
ように記述できます。
!file.isEmpty() && !file.startsWith(".")
も参照してください