Regex to select text excluding boundaries

I am trying to use the advanced find dialog to search for text enclosed by let’s say [ ] and apply a style to the text but not the boundary [ ] characters themselves.

So if I have

[This is a test. Hope it works.]

I want to search and select all text inside square brackets, not including the brackets themselves, and apply italics let’s say.

Can this be done using a search and replace regex instead of writing a macro?

Thanks.

What do you - exactly - mean by the term “advanced find dialog”?
I can tell you how to do it with the standard ‘F&R’ tool of ‘Writer’ in 3 runs.

@Lupp Sorry I meant the F&R dialog. I already worked out how to do it in three turns, 1) change entire string, 2) revert left side, 3) revert right side. But I was hoping for cleaner way…

OK, in the standard case we will only need 2 steps. I will describe the procedure in short in my answer to come for other visitors of the thread.

Hi

It is possible in one step… Find (?<=\[).*?(?=\])

Do not forget to tick ther Other optionsRegular expressions

Explanations

  • (?<= ... ) Look-behind assertion. True if the parenthesized pattern matches text preceding the current input position. Here (?<=\[) look for [
  • (?= ... ) Look-ahead assertion. True if the parenthesized pattern matches at the current input position, but does not advance the input position.

See ICU Regular expressions.

Regards

Awesome hint.

Confirmed!
It’s just a pity that the replacement does not work for me using this RegEx (V5.2.0). I cannot replace the serach result with anything whether changing the format nor leaving it unchanged.

Hi @Lupp

I reproduce the problem but the replacement is not needed here: just apply the style to the selection.

If replacement is necessary proceed in two steps, selection then replacement on the selection. You can also have one step if you copy first the replacement string (select then paste).

I found this bug but is slightly different

Regards

You can click Find all, close the dialog box and apply any formatting you like in one fell swoop. So not being able to replace anything isn’t very bad here.

@PYS, @anon87010807: You are right. However, it’s clearly a bug, isn’t it. I am really tired of filing bugs, especially in cases of such an “entanglement”. Would you expect results?

@Lupp - yes it is a bug (FYI it is ok in Calc). I thought to create a new report, but as I was going to use exactly the same title I have commented this one.

@PYS: (1.)Thanks for answering! (2.) I did not understand “this one” in your comment above.

(1. step:)
Call ‘F&R’
Search For: \[[^\]]+\]
(No font attributes)
Replace With: &
‘Other Options:’ > ‘Format…’ (Western Text Font): Only ‘Italic’ under ‘Style’.

(2. step)
Call ‘F&R’
Search For: \[|\]
(No font attributes)
Replace with: &
‘Other Options:’ > ‘Format…’ (Western Text Font): Only ‘Regular’ under ‘Style’.

The pairs of square brackets must be assured to not enclose a paragraph break.
If there also are square brackets used for different purposes things get more complicated, of course.

Great answer. Pity I cannot upvote you just yet… :slight_smile: