I have 2 linux commands called like this
ntpq -pn | sed -n '1,2!p' echo "%%%" chage -l root
The %%% is the delimiter I am using, for parsing the output.
The command above returns:
remote refid st t when poll reach delay offset jitter
============================================================================
*NTP_SERVER SOME_IP_HERE 4 u 36 1024 77 0.511 -4.415 197.945
%%%
Password inactive : never
Password expire : never\
The regex that I have %%%((?:.*)) works fine when I want to take only the output of the second command, but what if I have more outputs behind this one separated with %%%. I want to take them in different groups too.
How can I transform my regex pattern, so that it catches all the outputs in separate groups separated by the delimiter?
CodePudding user response:
Assuming you have a multiline string with some unimportant text before the first %%% delimiter, you can remove this unimportant text first and then split with %%%:
String[] entries = text.replaceFirst("(?s)^.*?%%%[\r\n]*", "").split("\\s*%%%\\s*");
See the first regex demo and the second regex demo.
Details:
(?s)-.now matches any chars (same asPattern.DOTALL)^- start of string.*?- any zero or more chars, as few as possible%%%- a%%%string[\r\n]*- zero or more CR or LF chars.
