How to parse these fields with single regex?

Selva :

I need to get the bold fields with a single regex

<103>CP-MGMT xpand[2859]: Configuration changed from localhost by user admin by the service dbset

<31>routed[4006]: rt_instance_monitor_job: scheduled next instance monitor in 5 seconds

<134>CP_FireWall: 2Jul2017 18:52:23 accept ip address message

I tried with following patterns

(?:<\d{1,3}>)\s*(\S+?)(?:\[\d*\])?:(.*) - failed for 1st log

(?:<\d{1,3}>)(?:\S*\s)?([^\[\]]+?)(?:\[\d*\])?:\s(.*) - failed for 2nd and 3rd logs

Please excuse any errors on my part. Thanks in advance

Emma :

This expression,

<\d{1,3}>\s*.*?(\S+?)(?:\[\d*\])?\s*:\s*(.*)

might simply work OK.

Test

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));
            }
        }

    }
}

Output

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

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=323489&siteId=1