我的公司里有一个非常奇怪的文件。它是由一个程序生成的,由很久以前的人编写。它包含许多属性以及具有该属性的用户。它看起来像下面。我必须逐行读取此文件,并将属性和用户分成两个列表。例如,在第一行中,所有道具应该在一个列表中,方括号内的用户应该在另一个列表中。用户数量一直在变化,这使我的任务变得复杂。有人建议我破解它吗?我开了几个小时而不是它。任何提示都会非常明显。
Properties,prop1,prop2,prop3,[user1,user2,user3,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,user2,user3,user4,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,user2,],prop4,prop5
Properties,prop1,prop2,prop3,[user1,user2,user3,user4,user5,],prop4,prop5
答案 0 :(得分:2)
我会用状态机编写一个简单的解析器,其中[
会将您切换为读取用户,]
会将您切换为读取属性。
另一种方法是以两种方式分割线条。
String line;
while((line = bufferedReader.readLine()) != null) {
for(String parts: line.split("\\[")) {
String[] userProps = parts.split("\\)");
String users = userProps.length==1 ? "" : userProps[0];
String props = userProps[userProps.length-1];
// break up the individual users and props
}
}