1

I'm trying to split a log line which looks like this:

15:10:10,408 INFO <NioProcessor-11> Raw Message: 8=FIXT.1.1|9=317|35=D|49=verifix|

into something like this:

15:10:10 
408
INFO
NioProcessor-11
FIXT.1.1
317
D
verifix

each information in split.

I have tried to split it with the split function with only

mystring.split((,)| \\\|) which splits my code to something like that:

15:10:10
408 INFO <NioProcessor-11> Raw Message: 8=FIXT.1.1
9=317
35=D
49=verifix

Then I took the second split 408 INFO <NioProcessor-11> Raw Message: 8=FIXT.1.1 and do a substring. It worked, but not perfectly because sometimes the second split is longer or shorter and it doesn't look to be a good solution.

I would like to know if it's possible to split that string with regex expressions actually. I'm new to regex and I didn't find a way to write an expression that handles my objective.

4
  • it would be helpful if you can mention few more sample of log lines in your logfiles. Currently, it only mentioned a single pattern of logs. Commented Mar 2, 2018 at 20:18
  • How about a single split [, <>]|(?: Raw Message: )?\|?\d+=|\| Commented Mar 2, 2018 at 20:21
  • 1
    Try splitting on (?:> [^:]+: |\d+=|[ ,|<>])+ Commented Mar 2, 2018 at 20:23
  • yes ctwheels it works tahnk you Commented Mar 2, 2018 at 20:49

4 Answers 4

1

See regex in use here

(?:> [^:]+: |\d+=|[ ,|<>])+
  • (?:)+ Match any of the following options one or more times
    • > [^:]+: Matches > , then one or more of any character except :, then :
    • \d+= Match one or more digits, then =
    • [ ,|<>] Match any character in the set

Result:

15:10:10
408
INFO
NioProcessor-11
FIXT.1.1
317
D
verifix
Sign up to request clarification or add additional context in comments.

Comments

0

if you search for a generic solution this could be helpful

    final String first = "15:10:10,408 INFO <NioProcessor-11> Raw Message: 8=FIXT.1.1|9=317|35=D|49=verifix|";
    final String second = "408 INFO <NioProcessor-11> Raw Message: 8=FIXT.1.1";

    String pattern = "[,| ]";
    Arrays.stream(first.split(pattern)).forEach(System.out::println);
    System.out.println("--------------------------");
    Arrays.stream(second.split(pattern)).forEach(System.out::println);

but if you want to get the specified elements, use groups and a specific pattern.

3 Comments

thank you for asking so your pattern split whether he find a( ',' space or pipe) what if i want to split these 8=FIXT.1.1|9=317 without those 8= & 9= =to=> FIXT.1.1 and 317 .. is there a way ?
there are a lot of tools around regex search for "regex coach" or "regex tester" and use the one you like most. even for IDE (Eclipse for example) there are plugins
youtube.com/… check this course
0

@alaaeddine zammel thats more specific

    final String input = "8=FIXT.1.1|9=317";
    final String groupPattern = "\\d=([A-Z\\.\\d]*)\\|\\d=([A-Z\\.\\d]*)";
    final Pattern p = Pattern.compile(groupPattern);
    final Matcher m = p.matcher(input);
    if (m.matches()) {
        System.out.println(m.group(1));
        System.out.println(m.group(2));
    }

Comments

0

Regex: (?:> Raw Message: |\|)?(?:\d+=|[ ,<|]+)

Details:

  • (?:> Raw Message: |\|)? Non-capturing group (?:) match > Raw Message: or | pipe \| if they exist ?
  • (?:\d+=|[ ,<|]+) Non-capturing group (?:) match digit one or unlimited times \d+ or | match a single character present in the list [ ,<|]+ one or unlimited times +

Java code:

String test = "15:10:10,408 INFO <NioProcessor-11> Raw Message: 8=FIXT.1.1|9=317|35=D|49=verifix|";
String[] data =test.split("(?:> Raw Message: |\\|)?(?:\\d+=|[ ,<|]+)");
for(String r : data) {
    System.out.println(r);
}

Output:

15:10:10
408
INFO
NioProcessor-11
FIXT.1.1
317
D
verifix

Code demo

5 Comments

@alaaeddine zammel Practice a lot!
any links or sources ?
@alaaeddine Just google. You can search right here with tag 'regex' and find a lot of simple examples that you can solve by your self. Use regex101 as a practice tool.
thank you sorry can't upvote your response ~~cause im new to stack i will google :)
@alaaeddine zammel No problem. Glad I could help. See regex section of SO.

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.