Find all within brackets

Some time ago Brian Barker provided a brilliant formula for finding all text between less-than and greater-than signs, namely

<[^>]*>

I’ve been trying to adapt it to finding text within brackets [thus], but without success. I know that brackets as such, as distinct from regular expressions, are identified as \[ and \]; so the formula I’ve come up with is

\[[^>]*\]

Unfortunately there are two things wrong with this: (1) it finds all text until a later closing bracket (seemingly the last one in the same paragraph) but not necessarily the immediately following one, and (2) the selection includes the brackets themselves, whereas I only want the text that they enclose.

Am I attempting the impossible? Any help gratefully received.

Some time ago Brian Barker provided a brilliant formula for finding all text between less-than and greater-than signs, namely
<[^>]*>

I've been trying to adapt it to finding text within brackets [thus], but without success. I know that brackets as such, as distinct from regular expressions, are identified as \[ and \]; so the formula I've come up with is
\[[^>]*\]

Unfortunately there are two things wrong with this: (1) it finds all text until a later closing bracket (seemingly the last one in the same paragraph) but not necessarily the immediately following one, ...

That central greater-than sign also needs to become a close bracket:
\[[^]]*\]

... and (2) the selection includes the brackets themselves, whereas I only want the text that they enclose. Am I attempting the impossible?

Yes and no. I don't see how you can match something that then doesn't include parts of the pattern. (Others may know better.) But there are things you can do:

o If you want to replace what is contained in the brackets but not the brackets themselves, you can reinsert them in the "Replace with" string, as "[something-else]" (no quotes).

o You can mark the brackets and their contents separately - using parentheses - and then use the parts in the replacement. Use
(\[)[^]]*(\])
and then "$1something-else$2" (no quotes) in the replacement. "$1" refers to the first parenthesised part and "$2" to the second - in this case the brackets themselves.

o Another technique would be to match the entire string, including the brackets, do what you want with that, and then perform another Find & Replace to correct the unwanted effect on the brackets. One idea might be to include additional brackets in the replacement, so that the result would be "[[something-else]]". Then it would be a simple task to search for those double brackets and do whatever was required.

It's difficult to be more precise without knowing exactly what you are trying to achieve.

I trust this helps.

Brian Barker

Thank you (once again), Brian. I’l test all this in the morning.

What I’m trying to achieve is to select all the terms inside the brackets and then in one fell swoop (1) to change them all to italic (and therefore excluding the brackets) and (2) to change the language of the text within the brackets to a different one from the main text.

I’ll do it all manually if necessary, but there are an awful lot of them!

S.

Interesting question, Seamas. I found a reference here
https://stackoverflow.com/a/27225148 and tried that on LibreOffice
Writer 6.4 and it seems it works:

(?<=\[).+?(?=\])

It can find your "[thus]" without the brackets.

No need. First use
\[[^]]*\]
and Find All to select all the bracketed phrases. Then apply italics and the change of language. Then search for
[|]
(meaning open bracket or close bracket) and again Find All to select all brackets. Now undo italics and (if it matters) reset the language for the brackets themselves.

I trust this helps.

Brian Barker

Thanks to everyone for your help. The job is now done, and it’s a pleasure to behold! I’m very grateful for all the help.

Séamas

Interesting question, Seamas. I found a reference here
https://stackoverflow.com/a/27225148 and tried that on LibreOffice
Writer 6.4 and it seems it works:

(?<=\[).+?(?=\])

It can find your "[thus]" without the brackets.

I've tried the various regex in this thread and this last one (above) produces an unusual result for me.

In the Find and Replace dialogue, if I press "Find Next" it finds the next occurence and highlights only the contents inside the brackets as expected. But the odd behaviour comes when I press "Replace" - it does not replace the contents but just finds the next occurrence.  In other words, the "Replace" button acts exactly the same as the "Find Next" button.

If I select "Replace All", it does just that.  I usually avoid the 'replace all' function because of the possibility of unexpected consequences, at least until after I have used the 'Find Next' and 'Replace' sequence a few times.

For the other regex in this thread, the "Find Next" and "Replace" buttons behave correctly.

I tried many times also using the alternative expression mentioned in the link above,

(?<=\[)[^]]+(?=\])  ,|and the result was always the same - in my LibreOffice Writer 6.4.5.2.

Philip