java - How to get only characters from a file in java
问题描述
I have a file txt. This is the file:
Team P W L D F A Pts
1. Arsenal 38 26 9 3 79 - 36 87
2. Liverpool 38 24 8 6 67 - 30 80
3. Manchester_U 38 24 5 9 87 - 45 77
4. Newcastle 38 21 8 9 74 - 52 71
5. Leeds 38 18 12 8 53 - 37 66
6. Chelsea 38 17 13 8 66 - 38 64
7. West_Ham 38 15 8 15 48 - 57 53
8. Aston_Villa 38 12 14 12 46 - 47 50
9. Tottenham 38 14 8 16 49 - 53 50
How can I get only the name of teams? I tried to use the regex in the following way but don't work:
FileReader f;
f=new FileReader("file.txt");
BufferedReader b;
b=new BufferedReader(f);
s=b.readLine();
String[] name = s.split("\\w+");
for(int i=0;i<name.length;i++)
System.out.println(name[i]);
How do I solve? Thanks to everyone in advance!
解决方案
FileReader f;
f=new FileReader("file.txt");
BufferedReader b;
b=new BufferedReader(f);
while(s=b.readLine()!=null){
Matcher name=Pattern.compile("(?<=\\d\\.\\s)\\S+").matcher(s);
if(name.find())
System.out.println(name.group());
}
here the regex (?<=\\d\\.\\s)\\S+
will match only the name after the serial no. Regex
推荐阅读
- swift - 带有 VFL 约束的场景 Dock 视图未显示
- python - PyQt5:QDoubleSpinBox 不允许负数
- javascript - 使用 vue-i18n 插件进行国际化
- java - Java:带有密钥的容器
- node.js - 如何在 Loopback 中向现有表中添加列
- java - Netty 4.0 ByteBuf 保留()使用
- angularjs - 主题标签和!,在网址的末尾。我怎样才能删除它,为什么它在那里。
- graphql - 在嵌套的 Prisma / GraphQL 查询中找不到参数
- java - 根据地图中的唯一键将地图条目列表分组为单个条目
- java - 如果 mime 类型未知,则确定消息正文类型