匹配行和提取文件名我有一個字符串以以下格式正則表達式在Java
Index: /aap/guru/asdte/atsAPI.tcl
===================================================================
RCS file: /autons/atsAPI.tcl,v
retrieving revision 1.41
Index: /aap/guru/asdte/atsAPI1.tcl
===================================================================
RCS file: /autons/atsAPI1.tcl,v
retrieving revision 1.41
我想要的是匹配行開始Index:
,然後得到路徑的文件名。
我的意思是先獲得Index: /aap/guru/asdte/atsAPI.tcl
,然後提取atsAPI.tcl
作爲最終結果。
目前我正在使用匹配兩次,第一整行,然後提取文件名。
我的問題是,如何在java中的單個正則表達式中做到這一點。
當前的代碼
String line = "Index: /aap/guru/asdte/atsAPI.tcl\r\n===================================================================\r\nRCS file: /autons/atsAPI.tcl,v\r\nretrieving revision 1.41\r\n\r\nIndex: /aap/guru/asdte/atsAPI1.tcl\r\n===================================================================\r\nRCS file: /autons/atsAPI1.tcl,v\r\nretrieving revision 1.41";
Pattern regex1 = Pattern.compile("Index:.*?\\n", Pattern.DOTALL);
Pattern regex2 = Pattern.compile("[^*/]+$");
Matcher matcher1 = regex1.matcher(line);
while (matcher1.find()) {
String s = matcher1.group(0);
Matcher matcher2 = regex2.matcher(s);
while (matcher2.find()) {
System.out.println(matcher2.group(0));
}
}
https://codebunk.com/b/128141613/ –
你不需要'DOTALL'標誌,它使你在你的不貪婪的點上添加一個換行符。 '.'默認與新行不匹配。 – revo
@g_p我已經包括瞭如何使用一個正則表達式以及我的答案中的工作演示。希望能幫助到你! – degant