0

I read from file like this:

List<String> list = Files.readAllLines(Paths.get("file.csv"));

After that I try to call replaceAll method on every string in the list, but it doesn't work with any regex and replacement string. Although, when I apply replaceAll with the same arguments to a string that I assign in code, it works fine. The strings look like this: "Hello","World","!!!"

Edit:

List<String> res = Files.readAllLines(Paths.get("TimeTable.csv"));

String p = "^\"(\\w+) (\\w+) (\\w+) (?:.+)?\",\"(\\d+)\\.(\\d+)\\.(\\d+)\",\"(\\d+):(\\d+):(\\d+)\"(?:.*?)$/i";
String rep = "$6-$5-$4 ==> $7:$8 $1 $2 $3";
String s = res.get(1).replaceAll(p, rep);
System.out.print(s);

The file consists of strings like that:

"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"

Here is the exact code I'm using: http://pastebin.com/GhhrRWAU

And here is the file I'm trying to parse: http://www.fileconvoy.com/dfl.php?id=g450e5a3e83854bdc999643999f2ceb8c622d6abf2

Denis Yakovenko
  • 3,241
  • 6
  • 48
  • 82
  • 1
    Please show your code that performs the `replaceAll`. – rgettman Mar 31 '15 at 20:19
  • Add some code. It will be easier to understand. – Razib Mar 31 '15 at 20:19
  • Sounds like you call `replaceAll` without writing the result back to the list? (Just a guess.) – tomse Mar 31 '15 at 20:23
  • Your code behaves the same for me whether I read that line in from a file, or assign it in-line (no replacement happens). Can you please show us the code where you assign the string in the code and it works? Perhaps you aren't escaping the `"` or something? – azurefrog Mar 31 '15 at 20:42
  • When I assign the string in the code I do escape all quotes – Denis Yakovenko Mar 31 '15 at 21:01

2 Answers2

1

After that I try to call replaceAll method on every string in the list

The replaceAll() method does not update the existing String. It creates a new String.

So you need to update the List with your newly created String:

String testing = "some text";
//testing.replaceAll(...); // this doesn't work
testing = testing.replaceAll(....);
camickr
  • 321,443
  • 19
  • 166
  • 288
1

RegEx is always a headache for me, too. Actually your regex expression is almost correct (you can check it on https://regex101.com/, for example). But this is Java and you should use inline modifier:

String p = "^\"(\\w+) (\\w+) (\\w+) (?:.+)?\",\"(\\d+)\\.(\\d+)\\.(\\d+)\",\"(\\d+):(\\d+):(\\d+)\"(?:.*?)$";
String rep = "$6-$5-$4 ==> $7:$8 $1 $2 $3";
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);

Output:

2015-02-23 ==> 07:40 AK Pz 310u

BTW, \i modifier is useless here, because \w already matches [a-zA-Z0-9_]

EDIT Your file contains non-Latin characters, so you can use Regex groups instead:

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 = "$6-$5-$4 ==> $7:$8 $1 $2 $3";
for (String str : res){
    System.out.println(str.replaceAll(p, rep));
}
nikis
  • 11,166
  • 2
  • 35
  • 45
  • it didn't work for me, unfortunately... And the cool trick is that on regex101.com everything works super fine, but not in code – Denis Yakovenko Mar 31 '15 at 21:31
  • @DenisYakovenko please check once again the pattern I've written carefully. Did you remove `/i` from the end? Here is a proof, that it works as expected https://ideone.com/W6Hv5Y. – nikis Mar 31 '15 at 21:35
  • Yes, your exapmle worked with the string assigned in the code. Although it doesn't work with the same strings from file – Denis Yakovenko Mar 31 '15 at 21:52
  • Here I've hosted the file I'm trying to use: http://www.fileconvoy.com/dfl.php?id=g450e5a3e83854bdc999643999f2ceb8c622d6abf2 And here is the exact code that I'm using: http://pastebin.com/GhhrRWAU – Denis Yakovenko Mar 31 '15 at 21:59
  • maybe it doesn't work with file because the file contains ciryllic values? – Denis Yakovenko Mar 31 '15 at 22:16
  • 1
    @DenisYakovenko definitely yes, because `\w` matches Latin characters. I suggest you to use `Regex` groups instead. I've updated my answer with new pattern. – nikis Apr 01 '15 at 06:21