首页 > 解决方案 > 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!

标签: javaregex

解决方案


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


推荐阅读