Regex to exclude everything but what's inside [ ] including line breaks

Tag: regex Author: johntow Date: 2013-10-03

I want to clean a text that has some notes in [ ]. I just want to have this notes and nothing else. There's some notes with line breaks, but I want to include that also. So in:

    lorem ipsum [foo
    bar] lorem ipsum

I want to exclude the lorem ipsum, but keep the [foo bar]. Anyone can tell me how to do it?

Other Answer1

Try this regex:

\[([\s\S]*?)\]

The captured group will have the text between the square brackets.

comments:

This one worked, but I realized I wanted something a little bit different. more like this: lorem ipsum "foo bar" [foo bar] lorem ipsum Where I would like to select just the "foo bar" [foo bar]
(".+?" [.+?])
It didn't work like that, but it worked like this: ("[\s\S]+?"[[\s\S]+?]). Thanks for the help, I'm pretty new at this stuff. One more thing, though: I want to exclude any thing like "foo bar" lorem ipsum [foo bar]. I just need to capture the lines where "foo bar" [foobar] are right next to each other and not when there's anything else in between
[\s\S]+ matches new line as well. For the second question please make it clearer. Perhaps start a new question topic.

Other Answer2

You can use this in many regex flavours:

\[[^]]+]

Other Answer3

This is pcre based. The m switch is for multi-line mode where . matches anything including newlines. I'm sure your regex flavour will have multi-line mode

~\[(.*?)\]~m