1

I have a string output that I am trying to capture a list of items from using regex matching. The string is as follows ...

ltm virtual test_vs {
    profiles {
        foo_bar {
            context all
        }
        baz {
            context one
        }
        qux {
            context all
        }
    }
}

What I want is do is come up with a regex that will match on the whole string and capture foo_bar, baz, and qux out of it, without knowing the values of these captures beforehand. Also, I want it to be flexible in that there can be any number of items to capture; they will just always be in between profiles brackets and each have open and closed brackets themselves with context ANYWORD in between their own brackets. What I have so far is ...

List<String> itemList = new ArrayList<>();
regex = "ltm\\svirtual\\stest_vs\\s\\{\\s*\\n\\s*profiles\\s\\{"+TODO;
pattern = Pattern.compile(regex);
matcher = pattern.matcher(output);
while(matcher.find()) {
    itemList.add(matcher.group(1));
}

Just need some help filling in the rest of the regex. Suggestions?

4
  • You'll want to use capture groups to be able to extract parts of the matched text. If you really have line breaks as shown you'll probably need the MULTILINE and DOTALL options to your pattern. Commented Oct 28, 2015 at 15:06
  • Can there be nested brackets also like qux { { anything } } ? Commented Oct 28, 2015 at 15:28
  • try the simpleJSON library Commented Oct 28, 2015 at 15:56
  • @anubhava No there can't be any nested brackets. Commented Oct 28, 2015 at 16:09

2 Answers 2

1

You can use this regex based on \G that asserts position at the end of the previous match or the start of the string for the first match:

String regex = "(?:\\bprofiles\\s*\\{|(?<!^)\\G)[^{]+?(\\b\\w+\\b)\\s*\\{[^}]*}";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(output);

List<String> itemList = new ArrayList<>();
while(matcher.find()) {
    itemList.add(matcher.group(1));
}

RegEx Demo

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

3 Comments

Hmmm, I copied and pasted your regex exactly and it's throwing the following exception: java.util.regex.PatternSyntaxException: Illegal repetition near index 15 (?:\bprofiles\s*{|(?<!^)\G)[^{]+?(\b\w+\b)\s*{[^}]*}
Are you able to compile that regex in Java? The demo clearly works though.
Check updated answer. Java needs { to be escaped as well.
0

([^\\s]+)\\s+\\{\\n\\s*context\\s if the input is really this simple. Lots of things can go wrong however, e.g. if one of the profiles is called "context".

Otherwise, it could be better to parse it (as JSON or writing your own parser) rather than use regex.

1 Comment

This isn't working for me. I'm trying to debug it in a regex tester.

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.