提取文本中的子字符串

我正在编写一个解析器,它在其中读取特定文件中的所有行并对其进行处理!!我陷入了这个子字符串Abc [123]的困境,其中ABC应该作为一个字符串存储,而123应该作为其他字符串存储。所以我想出了这个解决方案

lines.get(i).substring(0,lines.get(i).lastIndexOf("["))

这使我得到字符串abc

lines.get(i).substring(lines.get(i).lastIndexOf("["));

上面有人给我字符串123],但是我最后不希望]可以为我的方法做任何更新吗?

insertgon 回答:提取文本中的子字符串

更改

lines.get(i).substring(lines.get(i).lastIndexOf("["));

lines.get(i).substring(lines.get(i).lastIndexOf("[") + 1,lines.get(i).lastIndexOf("]"));
,

将正则表达式与组结合使用怎么办?

Pattern pattern = Pattern.compile("([A-Za-z ]{1,})([\[]{1})([0-9]{1,})");
for(String line : lines){
  Matcher matcher = pattern.matcher(line);
  while (matcher.find()) {
    System.out.println("group 1: " + matcher.group(1));
    System.out.println("group 3: " + matcher.group(3));
  }
}
本文链接:https://www.f2er.com/3041897.html

大家都在问