我是一個Java新手,我有一個包含類似下面信息的文本文件:Java表達式查找多個單詞
1.Store 123 has cloth style B.
2.Store 253 has cloth style D.
3.Store 27 has cloth style A.
4.Store 164 has cloth style F.
......
然後我試圖把有用的信息(只是門店數量和款式號)爲一組,並打印出來,設定應該是這樣的:
[123 B, 253 D, 27 A, 164 F, ...]
我嘗試這樣做:
Set<String> setStoreStyle = new HashSet<String>();
Pattern patternStoreStyle = Pattern.compile("Store (.+?) has cloth style(.+?).");
FileInputStream fstream = new FileInputStream(MyTextfilePath);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
for (String line = br.readLine(); line != null; line = br.readLine()){
Matcher matcherStoreStyle = patternStoreStyle.matcher(line);
if(matcherStoreStyle.find()){
String a = matcherStoreStyle.group(1);
setStoreStyle.add(a);
}
}
br.close();
System.out.println(setStoreStyle);
但我只是得到
[123, 253, 27, 164, ...]
任何想法解決它?
轉義最後一個點('\\。')並組合2個捕獲值。 –
訪問'matcherStoreStyle.group(2)'可能會有所幫助... –
非常感謝! –