-2

Given the following input:

something_[[foo.bar]]-[[foo.bar[bin=baz]]]something[[foobar]]

I'd like to capture the following substrings (anything between [[ and ]], including single square brackets):

[[foo.bar]]
[[foo.bar[bin=baz]]]
[[foobar]]

This regex gets part of the way there:

/(\[\[.+?\]\])/i

But it only captures the first instance: [[foo.bar]]. How can I make it capture the other two substrings?

Chris White
  • 789
  • 8
  • 18
  • It should match them all. Each match will just have one capture group. – Barmar Jun 02 '22 at 16:52
  • 1
    What language are you using, and what function? Some functions only return the first match, there's another function to return all matches. Or you have to use the `g` modifier to match all occurrences. – Barmar Jun 02 '22 at 16:52
  • E.g. PHP has `preg_match()` and `preg_match_all()`. Python has `re.search()` and `re.findall()`. JavaScript uses the `g` modifier with `.match()`, and also has `.matchAll()`. – Barmar Jun 02 '22 at 16:53
  • You have this question tagged `regex-greedy`, but `.*?` is non-greedy. – Barmar Jun 02 '22 at 16:54
  • @Barmar using the global flag worked! If you add this as an answer I'll accept it. Thanks! – Chris White Jun 02 '22 at 16:55
  • Even with global flag it will match `[[foo.bar[bin=baz]]` second time leaving last `]` out – anubhava Jun 02 '22 at 16:57
  • I can't answer until you add the language and code. – Barmar Jun 02 '22 at 16:59
  • Sure you can. Just say "use the global flag" in your answer. – Chris White Jun 02 '22 at 18:55

0 Answers0