为什么 replaceAll 方法不起作用?
Why replaceAll method is not working?
我是这样从文件中读取的:
List<String> list = Files.readAllLines(Paths.get("file.csv"));
之后,我尝试对列表中的每个字符串调用 replaceAll
方法,但它不适用于任何正则表达式和替换字符串。虽然,当我将具有相同参数的 replaceAll
应用于我在代码中分配的字符串时,它工作正常。
字符串如下所示:"Hello","World","!!!"
编辑:
List<String> res = Files.readAllLines(Paths.get("TimeTable.csv"));
String p = "^\"(\w+) (\w+) (\w+) (?:.+)?\",\"(\d+)\.(\d+)\.(\d+)\",\"(\d+):(\d+):(\d+)\"(?:.*?)$/i";
String rep = "-- ==> : ";
String s = res.get(1).replaceAll(p, rep);
System.out.print(s);
文件由这样的字符串组成:
"AK Pz 310u PI-13-5","23.02.2015","07:45:00","23.02.2015","09:20:00","False","True","23.02.2015","07:40:00","2","Common","AK Pz 310u PI-13-5","Common"
这是我使用的确切代码:http://pastebin.com/GhhrRWAU
这是我要解析的文件:http://www.fileconvoy.com/dfl.php?id=g450e5a3e83854bdc999643999f2ceb8c622d6abf2
After that I try to call replaceAll method on every string in the list
replaceAll()
方法不更新现有字符串。它创建一个新的字符串。
因此您需要使用新创建的字符串更新列表:
String testing = "some text";
//testing.replaceAll(...); // this doesn't work
testing = testing.replaceAll(....);
RegEx
也一直让我头疼。实际上,您的正则表达式几乎是正确的(例如,您可以在 https://regex101.com/ 上进行检查)。但这是 Java
,你应该使用内联修饰符:
String p = "^\"(\w+) (\w+) (\w+) (?:.+)?\",\"(\d+)\.(\d+)\.(\d+)\",\"(\d+):(\d+):(\d+)\"(?:.*?)$";
String rep = "-- ==> : ";
String test = "\"AK Pz 310u PI-13-5\",\"23.02.2015\",\"07:45:00\",\"23.02.2015\",\"09:20:00\",\"False\",\"True\",\"23.02.2015\",\"07:40:00\",\"2\",\"Common\",\"AK Pz 310u PI-13-5\",\"Common\"";
String s = test.replaceAll(p, rep);
System.out.print(s);
输出:
2015-02-23 ==> 07:40 AK Pz 310u
顺便说一句,\i
修饰符在这里没用,因为 \w
已经匹配 [a-zA-Z0-9_]
编辑 您的文件包含非拉丁字符,因此您可以改用 Regex
组:
List<String> res = Files.readAllLines(Paths.get("TimeTable.csv"));
String p = "(?i)^\"([\p{L}_]+) (\p{L}+) ([\p{L}\p{N}-_]+) (?:.+)?\",\"(\d+)\.(\d+)\.(\d+)\",\"(\d+):(\d+):(\d+)\"(?:.*?)$";
String rep = "-- ==> : ";
for (String str : res){
System.out.println(str.replaceAll(p, rep));
}
我是这样从文件中读取的:
List<String> list = Files.readAllLines(Paths.get("file.csv"));
之后,我尝试对列表中的每个字符串调用 replaceAll
方法,但它不适用于任何正则表达式和替换字符串。虽然,当我将具有相同参数的 replaceAll
应用于我在代码中分配的字符串时,它工作正常。
字符串如下所示:"Hello","World","!!!"
编辑:
List<String> res = Files.readAllLines(Paths.get("TimeTable.csv"));
String p = "^\"(\w+) (\w+) (\w+) (?:.+)?\",\"(\d+)\.(\d+)\.(\d+)\",\"(\d+):(\d+):(\d+)\"(?:.*?)$/i";
String rep = "-- ==> : ";
String s = res.get(1).replaceAll(p, rep);
System.out.print(s);
文件由这样的字符串组成:
"AK Pz 310u PI-13-5","23.02.2015","07:45:00","23.02.2015","09:20:00","False","True","23.02.2015","07:40:00","2","Common","AK Pz 310u PI-13-5","Common"
这是我使用的确切代码:http://pastebin.com/GhhrRWAU
这是我要解析的文件:http://www.fileconvoy.com/dfl.php?id=g450e5a3e83854bdc999643999f2ceb8c622d6abf2
After that I try to call replaceAll method on every string in the list
replaceAll()
方法不更新现有字符串。它创建一个新的字符串。
因此您需要使用新创建的字符串更新列表:
String testing = "some text";
//testing.replaceAll(...); // this doesn't work
testing = testing.replaceAll(....);
RegEx
也一直让我头疼。实际上,您的正则表达式几乎是正确的(例如,您可以在 https://regex101.com/ 上进行检查)。但这是 Java
,你应该使用内联修饰符:
String p = "^\"(\w+) (\w+) (\w+) (?:.+)?\",\"(\d+)\.(\d+)\.(\d+)\",\"(\d+):(\d+):(\d+)\"(?:.*?)$";
String rep = "-- ==> : ";
String test = "\"AK Pz 310u PI-13-5\",\"23.02.2015\",\"07:45:00\",\"23.02.2015\",\"09:20:00\",\"False\",\"True\",\"23.02.2015\",\"07:40:00\",\"2\",\"Common\",\"AK Pz 310u PI-13-5\",\"Common\"";
String s = test.replaceAll(p, rep);
System.out.print(s);
输出:
2015-02-23 ==> 07:40 AK Pz 310u
顺便说一句,\i
修饰符在这里没用,因为 \w
已经匹配 [a-zA-Z0-9_]
编辑 您的文件包含非拉丁字符,因此您可以改用 Regex
组:
List<String> res = Files.readAllLines(Paths.get("TimeTable.csv"));
String p = "(?i)^\"([\p{L}_]+) (\p{L}+) ([\p{L}\p{N}-_]+) (?:.+)?\",\"(\d+)\.(\d+)\.(\d+)\",\"(\d+):(\d+):(\d+)\"(?:.*?)$";
String rep = "-- ==> : ";
for (String str : res){
System.out.println(str.replaceAll(p, rep));
}