java - Why replaceAll method is not working? -
i read file this:
list<string> list = files.readalllines(paths.get("file.csv")); after try call replaceall method on every string in list, doesn't work regex , replacement string. although, when apply replaceall same arguments string assign in code, works fine. strings 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 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 exact code i'm using: http://pastebin.com/ghhrrwau
and here file i'm trying parse: http://www.fileconvoy.com/dfl.php?id=g450e5a3e83854bdc999643999f2ceb8c622d6abf2
regex headache me, too. regex expression correct (you can check on https://regex101.com/, example). java , 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 useless here, because \w matches [a-za-z0-9_]
edit file contains non-latin characters, 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"; (string str : res){ system.out.println(str.replaceall(p, rep)); }
Comments
Post a Comment