Java中的正则表达式模式匹配
请帮助我完成以下工作:Java中的正则表达式模式匹配,java,regex,match,filenames,Java,Regex,Match,Filenames,请帮助我完成以下工作: if the filename is like *0.txt, a++; if the filename is like *2.txt, b++; if the filename is like *4.txt, c++; 我在一个数组中有许多文件,如下所示: jack+0.txt jack+2.txt jack+4.txt tim+0.txt tim+2.txt tim+4.txt raph+0.txt raph+2.txt raph+4.txt wells+0.txt
if the filename is like *0.txt, a++;
if the filename is like *2.txt, b++;
if the filename is like *4.txt, c++;
我在一个数组中有许多文件,如下所示:
jack+0.txt
jack+2.txt
jack+4.txt
tim+0.txt
tim+2.txt
tim+4.txt
raph+0.txt
raph+2.txt
raph+4.txt
wells+0.txt
wells+2.txt
wells+4.txt
etc.
我想编写一个程序来执行以下操作:
if the filename is like *0.txt, a++;
if the filename is like *2.txt, b++;
if the filename is like *4.txt, c++;
需要的核心帮助是使用正则表达式(在Java中)。如果您有少量不同的结尾,请参考此想法:
if (filename.endsWith("0.txt")) a++;
如果你有少量不同的结尾,你的想法是:
if (filename.endsWith("0.txt")) a++;
我只需要在加号上进行字符串拆分,然后检查第二部分
String[] parts = s.split("+");
if (parts[1].equals("0.txt") {
} else // the rest of the tests
我只需要在加号上进行字符串拆分,然后检查第二部分
String[] parts = s.split("+");
if (parts[1].equals("0.txt") {
} else // the rest of the tests
这将去掉文件名中的数字,然后您可以进行切换
string filename;
int category = Integer.parseInt(filename.substring(filename.length() - 5, filename.length() - 4))
这将去掉文件名中的数字,然后您可以进行切换
string filename;
int category = Integer.parseInt(filename.substring(filename.length() - 5, filename.length() - 4))
可能是最简单的方法可能是最简单的方法如果文件将来有多个数字呢?如果文件将来有多个数字呢?谢谢大家,我正在尝试建议的方法,很快就会回来。谢谢大家,我正在尝试建议的方法,很快就会回来。谢谢大家,丹尼尔,我脱帽致敬。这个主意对我有用。非常感谢!谢谢大家,丹尼尔,脱帽致敬。这个主意对我有用。非常感谢!