0

Given String:

<180>May 19 17:54:27 ise121 CISE_Internal_Operations_Diagnostics 0000046406 2 0 2015-05-19 17:54:27.930 +05:30 0000290168 34126 WARN  System-Management: Remote syslog target is unavailable

My RegEx :

\\<(\\d+)\\>(\\w+[\\d\\s\\:]+\\S+\\s\\S+\\s(\\d+)\\s(\\d+)\\s(\\d+)\\s([\\d\\:\\-\\.\\+\\s]{0,30})\\s+\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\S+\\:.*?\\,)(.*)

but this is not working so please help

Code:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatchMoreLogsFromSingleRegEx {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = "<180>May 19 17:54:27 ise121 CISE_Internal_Operations_Diagnostics 0000046406 2 0 2015-05-19 17:54:27.930 +05:30 0000290168 34126 WARN  System-Management: Remote syslog target is unavailable";

        Pattern pattern = Pattern
                .compile("\\<(\\d+)\\>(\\w+[\\d\\s\\:]+\\S+\\s\\S+\\s(\\d+)\\s(\\d+)\\s(\\d+)\\s([\\d\\:\\-\\.\\+\\s]{0,30})\\s+\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\S+\\:.*?\\,)(.*)");

        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            System.out.println("true");
            StringBuilder sb = new StringBuilder();
            int cnt = matcher.groupCount();
            System.out.println(cnt);
            for (int i = 1; i <= cnt; ++i) {
                System.out.println(matcher.group(i) + "\t" + i);
            }

        } else {
            System.out.println("false");
        }
    }
}
2
  • What should be the output? Commented May 21, 2015 at 7:04
  • Please add some infromations, because you don't said what you would like to extract from this String? Commented May 21, 2015 at 7:04

1 Answer 1

2

It fails to work because there isn't a comma exists after the substring System Management: . So remove the last comma.

\\<(\\d+)\\>(\\w+[\\d\\s\\:]+\\S+\\s\\S+\\s(\\d+)\\s(\\d+)\\s(\\d+)\\s([\\d\\:\\-\\.\\+\\s]{0,30})\\s+\\d+)\\s+(\\d+)\\s+(\\S+)\\s+(\\S+\\:.*?)(.*)

DEMO

I think you want something like this. Here I made the comma part as optional.

\<(\d+)\>(\w+[\d\s\:]+\S+\s\S+\s(\d+)\s(\d+)\s(\d+)\s([\d\:\-\.\+\s]{0,30})\s+\d+)\s+(\d+)\s+(\S+)\s+(\S+\:(?:.*?,)?)(.*)

Escape the backslash one more time in java.

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.