Necesito conseguir los llamativos campos con una sola expresión regular
<103> CP-MGMT xpand [2859]: Configuración modificada de localhost por admin usuario por el servicio de dbset
<31> enrutado [4006]: rt_instance_monitor_job: programado monitor siguiente ejemplo, en 5 segundos
<134> CP_FireWall : 18:52:23 2Jul2017 mensaje de aceptación de la dirección IP
Probé con los patrones siguientes
(?:<\d{1,3}>)\s*(\S+?)(?:\[\d*\])?:(.*) - failed for 1st log
(?:<\d{1,3}>)(?:\S*\s)?([^\[\]]+?)(?:\[\d*\])?:\s(.*) - failed for 2nd and 3rd logs
Por favor, disculpe cualquier error de mi parte. Gracias por adelantado
Esta expresión,
<\d{1,3}>\s*.*?(\S+?)(?:\[\d*\])?\s*:\s*(.*)
podría simplemente funcionar bien.
Prueba
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class re{
public static void main(String[] args){
final String regex = "<\\d{1,3}>\\s*.*?(\\S+?)(?:\\[\\d*\\])?\\s*:\\s*(.*)";
final String string = "<103>CP-MGMT xpand[2859]: Configuration changed from localhost by user admin by the service dbset\n\n"
+ "<31>routed[4006]: rt_instance_monitor_job: scheduled next instance monitor in 5 seconds\n\n"
+ "<134>CP_FireWall: 2Jul2017 18:52:23 accept ip address message";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Salida
Full match: <103>CP-MGMT xpand[2859]: Configuration changed from localhost by user admin by the service dbset
Group 1: xpand
Group 2: Configuration changed from localhost by user admin by the service dbset
Full match: <31>routed[4006]: rt_instance_monitor_job: scheduled next instance monitor in 5 seconds
Group 1: routed
Group 2: rt_instance_monitor_job: scheduled next instance monitor in 5 seconds
Full match: <134>CP_FireWall: 2Jul2017 18:52:23 accept ip address message
Group 1: CP_FireWall
Group 2: 2Jul2017 18:52:23 accept ip address message
Si desea simplificar / Modificar / explorar la expresión, que ha sido explicado en el panel superior derecha de regex101.com . Si desea, también se puede ver en este enlace , la forma en que se correspondería en contra de algunas entradas de ejemplo.
Circuito RegEx
jex.im visualiza expresiones regulares: