2

Okay so i want to separate certain assignments into groups out of a string with the help of a regular expression in java.

My String looks like this:

LogEventDefinition(
logEvent = LogEvent.MESSAGE700,
process = process.Data,
processStep = "action")

my final solution should group it like this:

group 1      group 2
logEvent     LogEvent.MESSAGE700
process      process.Data
processStep  action

My idea is this:

(?=@LogEventDefinition\()(\w+)\s=(\s\w+)|(?:\s\w+\.(\w+))\\)

I do a lookahead and then check the following content of the String LogEventDefinition from the first bracket on. Essentially it is totally wrong, tried a few other attempts but nothing works, would appreciate your help a lot.

4
  • I think you need more than a regex for accomplishing this. May I know why you wanna use regex? Maybe a simple code can generate this output without regex. Oct 5, 2017 at 10:06
  • Well essentially because the software designed demands it, at least a regular expression for just extracting these exact patterns would at the end cause much less confusion within the code for extensions.
    – Leo
    Oct 5, 2017 at 10:07
  • Always remember if regex is used on a very large string or a file it may cause efficiency loss. Oct 5, 2017 at 10:09
  • " the software designed demands it" - you might want to elaborate on this as it will definitely help to better understand the requirements. As an example, Java regex doesn't support capturing multiple matches into a group, i.e. you'd have to apply the regex multiple times. Whether that's a viable approach or not depends on what the software does - and if you're free to use some code a non-regex expression might be even easier to understand.
    – Thomas
    Oct 5, 2017 at 10:09

1 Answer 1

2

I think you could even handle this without setting up a formal regex:

String input  = "LogEventDefinition(";
       input += "logEvent = LogEvent.MESSAGE700,";
       input += "process = process.Data,";
       input += "processStep = \"action\")";

input = input.replaceAll("\\w+\\((.*)\\)", "$1");  // remove function wrapper
String[] parts = input.split(",\\s*");             // split terms by comma
List<String> group1 = new ArrayList<>();
List<String> group2 = new ArrayList<>();

for (String part : parts) {
    group1.add(part.split("\\s*=\\s*")[0]);        // assign group1 term
    group2.add(part.split("\\s*=\\s*")[1]);        // assign group2 term
}

System.out.println("group1, group2");
for (int i=0; i < group1.size(); ++i) {
    System.out.println(group1.get(i) + ", " + group2.get(i));
}

As a side note, I can't figure out if you want to remove quotes around terms, or whether any of your original data has quotes in the first place. If you want to remove quotes, I can make a tiny fix to the above code to handle this requirement.

Output:

group1, group2
logEvent, LogEvent.MESSAGE700
process, process.Data
processStep, "action"

Demo here:

Rextester

4
  • Indeed i did realize its much easier by just slpitting the string by some symbols and then recollecting the parts in a list or anything, but the problem is that due to coding constraints i will have to find a regular expression for this. If only grouping of multiple matches is not possible in java, then it is okay for me not finding a regular expression. where did you find out that grouping multiple matches in java is not possible?
    – Leo
    Oct 5, 2017 at 11:00
  • Thanks for the Rextester, ill have a look at that one!
    – Leo
    Oct 5, 2017 at 11:01
  • Okay so i found an expression to easily extract just the content within the brackets: (?<=@LogEventDefinition\()([^\)]*) Is there any way now to continue working with the results of this regex with another regex, e.g. a pipe in unix? expression->regex1->result of regex 1 on on expression->regex2->result of regex2 on result of regex1
    – Leo
    Oct 5, 2017 at 11:16
  • Yes you could use a formal Java pattern matcher. But I actually prefer using String#split because it is more terse and easier to read. Oct 5, 2017 at 14:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.