import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(^\\w{3} \\d{1,2} [0-9:]{8}) (\\b\\w+[.]\\w*\\b) +(\\b\\w*\\b): (\\b\\w+\\b) ([\\w' ]*).*\\(([\\w.]*)\\)$";
final String string = "Jan 31 00:09:39 ubuntu.local ticky: INFO Created ticket [#4217] (mdouglas)\n"
+ "Jan 31 00:16:25 ubuntu.local ticky: INFO Closed ticket [#1754] (noel)\n"
+ "Jan 31 00:21:30 ubuntu.local ticky: ERROR The ticket was modified while updating (breee)\n"
+ "Jan 31 00:44:34 ubuntu.local ticky: ERROR Permission denied while closing ticket (ac)\n"
+ "Jan 31 01:00:50 ubuntu.local ticky: INFO Commented on ticket [#4709] (blossom)\n"
+ "Jan 31 01:29:16 ubuntu.local ticky: INFO Commented on ticket [#6518] (rr.robinson)\n"
+ "Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh)\n"
+ "Jan 31 01:43:10 ubuntu.local ticky: ERROR Tried to add information to closed ticket (jackowens)\n"
+ "Jan 31 01:49:29 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mdouglas)\n"
+ "Jan 31 02:30:04 ubuntu.local ticky: ERROR Timeout while retrieving information (oren)\n"
+ "Jan 31 02:55:31 ubuntu.local ticky: ERROR Ticket doesn't exist (xlg)\n"
+ "Jan 31 03:05:35 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)\n"
+ "Jan 31 03:08:55 ubuntu.local ticky: ERROR Ticket doesn't exist (blossom)\n"
+ "Jan 31 03:39:27 ubuntu.local ticky: ERROR The ticket was modified while updating (bpacheco)\n"
+ "Jan 31 03:47:24 ubuntu.local ticky: ERROR Ticket doesn't exist (enim.non)\n"
+ "Jan 31 04:30:04 ubuntu.local ticky: ERROR Permission denied while closing ticket (rr.robinson)\n"
+ "Jan 31 04:31:49 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)\n"
+ "Jan 31 04:32:49 ubuntu.local ticky: ERROR Timeout while retrieving information (mcintosh)\n"
+ "Jan 31 04:44:23 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)\n"
+ "Jan 31 04:44:46 ubuntu.local ticky: ERROR Connection to DB failed (jackowens)\n"
+ "Jan 31 04:49:28 ubuntu.local ticky: ERROR Permission denied while closing ticket (flavia)\n"
+ "Jan 31 05:12:39 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)\n"
+ "Jan 31 05:18:45 ubuntu.local ticky: ERROR Tried to add information to closed ticket (sri)\n"
+ "Jan 31 05:23:14 ubuntu.local ticky: INFO Commented on ticket [#1097] (breee)\n"
+ "Jan 31 05:35:00 ubuntu.local ticky: ERROR Connection to DB failed (nonummy)\n"
+ "Jan 31 05:45:30 ubuntu.local ticky: INFO Created ticket [#7115] (noel)\n"
+ "Jan 31 05:51:30 ubuntu.local ticky: ERROR The ticket was modified while updating (flavia)\n"
+ "Jan 31 05:57:46 ubuntu.local ticky: INFO Commented on ticket [#2253] (nonummy)\n"
+ "Jan 31 06:12:02 ubuntu.local ticky: ERROR Connection to DB failed (oren)";
final String subst = " \\1 \\2";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html