[英]How can i split on a string
我有一个通过 bufferReader 浏览的 .txt 文件,我需要从这个字符串中提取最后一个字符,我留下下面的行
<path
action="m"
text-mod="true"
mods="true"
kind="file">branches/RO/2021Align01/CO/DGSIG-DAO/src/main/java/eu/ca/co/vo/CsoorspeWsVo.java</path>
我有以下代码将我的整行代码设置在一个列表中,但我只需要它 Cs00rspeWsVo
while ((line = bufferdReader.readLine()) != null) {
Excel4 excel4 = new Excel4();
if (line.contains("</path>")) {
int index1 = line.indexOf(">");
int index2 = line.lastIndexOf("<");
line = line.substring(index1, index2);
excel4.setName(line);
listExcel4.add(excel4);
}
}
我只想从这里提取 Cs00rspeWsVo。 谁能帮我? 谢谢
例如,您可以使用正则表达式组来获取它
public static void main(String []args){
String input = "<path\n" +
" action=\"m\"\n" +
" text-mod=\"true\"\n" +
" mods=\"true\"\n" +
" kind=\"file\">branches/RO/2021Align01/CO/DGSIG-DAO/src/main/java/eu/ca/co/vo/CsoorspeWsVo.java</path>\n";
Pattern pattern = Pattern.compile("kind=\"file\">.+/(.+\\..+)</path>");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String fileName = matcher.group(1);
System.out.println(fileName);
}
}
Output 将是 -> CsoorspeWsVo.java
如果您想要填充路径,请将正则表达式更改为
Pattern pattern = Pattern.compile("kind=\"file\">(.+)</path>");
output 将是:
branches/RO/2021Align01/CO/DGSIG-DAO/src/main/java/eu/ca/co/vo/CsoorspeWsVo.java
例如,您可以在两组中获取名称和扩展名
Pattern pattern = Pattern.compile("kind=\"file\">.+/(.+)\\.(.+)</path>");
在 if 里面
String fileName = matcher.group(1);
String fileExtension = matcher.group(2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.