我的输入是这样的:
line 1
**ER1.RIAA.SOMPSFIO(LIAOEE) UTGD788 FDSJOFUZZÄ line2
JNDJZSDS ER1.RIAA.SIMEDFUA(AUDD) YIRIHFIH1465EZZÄ
line 3
UJZRJOERERÃLDE,UIE='UJ1.DHZKZ5.OZDEZN98.AAERRE',I=DZEDE POPZEOE**
我只想得到带有句点的字符,即:
ER1.RIAA.SOMPSFIO
ER1.RIAA.SIMEDFUA
UJ1.DHZKZ5.OZDEZN98.AAERRE
我的解决方案是:
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
nrligne++;
int counter = 0;
for (int i=0; i<line.length(); i++ ) {
if( line.charAt(i) == '.' ) {
counter++;
}
}
if (counter == 2) {
if (line.matches("^.*[A-Z0-9]+\\..[A-Z1-9]+.*$")){
line= removeTroublesomeCharacters(line);
System.out.println("ligne vaut "+line);
Pattern dsnPattern = Pattern.compile("^.*([A-Z0-9]+)\\..([A-Z1-9]+)\\..([A-Z1-9]+).*$");
Matcher m = dsnPattern.matcher(line);
if (m.matches()) {
String part1 = m.group(1);
String part2 = m.group(2);
String part3 = m.group(3);
System.out.println("part1 vaut "+part1);
System.out.println("part2 vaut "+part2);
System.out.println("part2 vaut "+part3);
}
}
目前的结果是
ligne vaut ER1.RIAA.SOMPSFIO(LIAOEE) UTGD788
part1 vaut 1
part2 vaut IAA
part2 vaut OMPSFIO
ligne vaut PZFDSJOFUZZÃâ ER1.RIAA.SIMEDFUA(AUDD) UOOO88
part1 vaut 1
part2 vaut IAA
part2 vaut IMEDFUA
ligne vaut UJZRJOERERÃLDE,UIE='UJ1.DHZKZ5.OZDEZN98',I=DZEDE POPZEOE
part1 vaut 1
part2 vaut HZKZ5
part2 vaut ZDEZN98
输入文件:http: //uploadhero.co/dl/PWBLhi7d 我不明白为什么正则表达式会吃掉每个字符的开头?有人可以帮我解决这个问题吗?