regex - How to get substring with pattern using Java -
i have file containing records below:
drwxr-xr-x - root supergroup 0 2015-04-05 05:26 /user/root drwxr-xr-x - hadoop supergroup 0 2014-11-05 11:56 /user/root/input drwxr-xr-x - hadoop supergroup 0 2014-11-05 03:06 /user/root/input/foo drwxr-xr-x - hadoop supergroup 0 2015-04-28 03:06 /user/root/input/foo/bar drwxr-xr-x - hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706 -rw-r--r-- 3 hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706/_success drwxr-xr-x - hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706/_logs drwxr-xr-x - hadoop supergroup 0 2013-11-06 15:54 /user/root/input/foo/bar/20120706/_logs/history
in java code, use pattern
, matcher
class substrings want process later. code in listing:
string filename = "d:\\temp\\files_in_hadoop_temp.txt"; pattern thepattern = pattern.compile("[a-z\\-]+\\s+(\\-|[0-9]) (root|hadoop)\\s+supergroup\\s+([0-9]+) ([0-9\\-]+) ([0-9:]+) (\\d+)\\/?.*"); try { files.lines(paths.get(filename)) .map(line -> thepattern.matcher(line)) .collect(collectors.tolist()) .foreach(themather -> { if (themather.find()) { system.out.println(themather.group(3) + "-" + themather.group(4) + "-" + themather.group(6)); } }); } catch (ioexception e) { e.printstacktrace(); }
and result below:
0-2015-04-05-/user/root 0-2014-11-05-/user/root/input 0-2014-11-05-/user/root/input/foo 0-2015-04-28-/user/root/input/foo/bar 0-2013-11-06-/user/root/input/foo/bar/ 0-2013-11-06-/user/root/input/foo/bar/ 0-2013-11-06-/user/root/input/foo/bar/ 0-2013-11-06-/user/root/input/foo/bar/
but expected results without tailing "/" first 3 rows. have tried many patterns strip tailing "/" failed.
would please provide suggestions pattern strip tailing "/".
thank lot.
use character set make sure last character isn't slash. thus, instead of
(\\d+)\\/?.*"
try
(\\d*[^\\d/]).*
the part in parentheses matches longest substring of nondigits, added restriction last character may not slash.
note: tested.
Comments
Post a Comment