1

I am trying to find array-like occurrences in a string.

For instance for this text

Ut ac nisi eget est tempus mattis. Sed et dapibus lorem. Suspendisse laoreet ante arcu, sed ornare a(diam)[test] ornare eget. Nunc a(pulvinar)[anoter][test] porttitor accumsan. Donec quis accumsan enim.Ut sed sem posuere, a(pharetra)[another[nested][a(test)]] sapien a, molestie odio. Donec euismod, lectus et sollicitudin condimentum, felis dolor feugiat arcu

i want to match bold parts.

i got this far:

\a\((.*?)\)(\[.*?])+

this matches first two, but the last test has the last closing bracket missing.(If i nest once more 2 closing brackets becomes missing)

results:

a(diam)[test]
a(pulvinar)[anoter][test]
a(pharetra)[another[nested][a(test)] <--- last closing bracket missing.

any help?

5
  • You might know about this already, but: cyber-reality.com/regexy.html Has been a great help for me when building regexes Commented Oct 8, 2013 at 13:20
  • You can't do that with regex (at least, JS flavor). Commented Oct 8, 2013 at 13:34
  • ended up with \a((.*?))((?:[.*?]+)+). Commented Oct 8, 2013 at 13:50
  • @thg435: it's posibble to match but i guess it's not possible to capture every [] seperately when they are nested. Commented Oct 8, 2013 at 13:51
  • @Jon, thanks. i am using regexpal.com but it's always useful to have a cheat sheet like that handy. Commented Oct 8, 2013 at 13:52

1 Answer 1

1

How about embedding the nested structure in regular expression like this:

a\(\w+\)(\[.+?(\[.+\])*\])+
              ---------  
              embeded nesting

EDIT:

(a\(\w+\)(?:\[.+?(?:\[.+\])*\])+)

Added non-capturing symbols to mitigate "undefined" captures.

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

3 Comments

Thanks! Everyting is fine, but of course it captures "undefined" when there is no nesting. I am guessing capturing everything between every [] seperately is not possible with js regex. i ended up with \a((.*?))((?:[.*?]+)+). only added + after last ] in my original regex and changed capturing groups. Capturing this way is mot logical for my use case.
I edited my answer. Now there is only one group for capturing whole text.
Used slightly modified version of your edited expression in the end. My version was not properly matching if there was nothing between [].

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.