import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?m)^(\\d{10}):\\s+(.+)";
final String string = "1652855102: New connection from xx.xx.xx.xx on port xxxx.\n"
+ "1652855102: Socket error on client <unknown>, disconnecting.\n"
+ "1652855102: Received PUBLISH from 16838547124974742985 (d0, q1, r0, m235, 's/uc/mpd/16838547124974742985', ... (30 bytes))\n"
+ "1652855102: Sending PUBACK to 16838547124974742985 (m235, rc0)\n"
+ "1652855102: Sending PUBLISH to mqtt_7811e829.e2e5e8 (d0, q1, r0, m42277, 's/uc/mpd/16838547124974742985', ... (30 bytes))\n"
+ "1652855102: Sending PUBLISH to dad3d73-013c-4274-a782-cdd6f2ebbc77 (d0, q0, r0, m0, 's/uc/mpd/16838547124974742985', ... (30 bytes))\n"
+ "1652855102: Received PUBACK from mqtt_7811e829.e2e5e8 (Mid: 42277, RC:0)\n"
+ "1652855103: Received DISCONNECT from 16838547082470259932\n"
+ "1652855103: Client 16838547082470259932 disconnected.";
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