Search code examples
javaregextokendelimiter

Regex based on THIS or THAT


I am trying to parse the below:

"#SenderCompID=something\n" +
"TargetCompID=something1"

into an array of:

{"#SenderCompID=something", "TargetCompId", "something1"}

Using:

String regex = "(?m)" + "(" +     
    "(#.*) |" +                //single line of (?m)((#.*)|([^=]+=(.+))
    "([^=]+)=(.+) + ")";
String toMatch = "#SenderCompID=something\n" +
    "TargetCompID=something1";

which is outputting:

#SenderCompID=something
null
#SenderCompID
something
                       //why is there any empty line here?
TargetCompID=something1
null
                       //why is there an empty line here?
TargetCompID
something1

I understand what I'm doing wrong here. The 1st group is returning the entire line, the 2nd group is returning (#.*) if the line starts with # and null otherwise, the 3rd group is returning ([^=]+=(.+). The | is what I'm trying to do. I want to parse it based on EITHER the conditions for the 2nd group

(#.*)

or for the 3rd group

([^=]+)=(.+).

How?

EDIT: miswrote example code


Solution

  • You can use this regex to get all 3 groups:

    (?m)^(#.*)|^([^=]+)=(.*)
    

    RegEx Demo

    RegEx Breakup:

    • (?m): Enable MULTILINE mode
    • ^(#.*): match a full line starting with # in group #1
    • |: OR
    • ^([^=]+)=: Match till = and capture in group #2 followed by =
    • (.*): Match rest of line in group #3