2012-10-11 92 views
0
匹配

請我需要幫助完成以下事項:正則表達式模式在Java中

我有許多文件,就像在陣列中的以下內容:

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中)。

+0

謝謝你們,我嘗試了建議的方法和將在短時間內恢復。 – bdfios

回答

1
for (File file : files) { 
    if (file.getName().matches(".*0[.]txt") { 
     a++; 
    } else if ... 
} 
+0

謝謝大家,丹尼爾,我的帽子掉了。這個想法適合我。謝謝一堆! – bdfios

2

的想法,如果你有少量不同的結局:

if (filename.endsWith("0.txt")) a++; 
+0

可能是最簡單的方法 –

2

我只想做加號將字符串分割,並檢查第二部分。

String[] parts = s.split("+"); 
if (parts[1].equals("0.txt") { 

} else // the rest of the tests 
0

這港島線把數出來的文件名,然後你可以做一個開關的情況下

string filename; 

int category = Integer.parseInt(filename.substring(filename.length() - 5, filename.length() - 4)) 
+0

如果文件將來有多個號碼會怎麼樣? – Gamb