1

I am trying to extract the values between `[::[' and ']::]'. The problem I am having is there are multiple instances of this in the same string and it is only picking up the first one. Any help with my regex? here is my code:

Sample input: line = "TEST [::[NAME]::] HERE IS SOMETHING [::[DATE]::] WITH SOME MORE [::[Last]::]";

Pattern p = Pattern.compile("\\[::\\[(.*?)\\]::\\]");
Matcher m = p.matcher(line);
if (m.find()) {
    System.out.println(m.group(1));
}

1 Answer 1

6

Your regex is OK. What you need to do is cycle through the matches, a Matcher can match several times!

while (m.find())
    System.out.println(m.group(1));

A Matcher will try again from the end of the last match (unless you use \G but that's pretty special)

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

1 Comment

Worked perfect. Awesome. Thank You!

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.