Ask Your Question

Regex to select text excluding boundaries [closed]

asked 2016-09-16 17:57:39 +0200

CaveMan gravatar image

updated 2020-07-12 00:16:43 +0200

Alex Kemp gravatar image

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?


edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-09-12 13:39:30.702364


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 gravatar imageLupp ( 2016-09-16 18:38:52 +0200 )edit

@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...

CaveMan gravatar imageCaveMan ( 2016-09-16 20:05:53 +0200 )edit

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.

Lupp gravatar imageLupp ( 2016-09-16 20:23:25 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2016-09-17 08:03:00 +0200

pierre-yves samyn gravatar image


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

Do not forget to tick ther Other optionsRegular expressions


  • (?<= ... ) 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.



edit flag offensive delete link more


Awesome hint.

floris v gravatar imagefloris v ( 2016-09-17 08:59:59 +0200 )edit

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.

Lupp gravatar imageLupp ( 2016-09-17 16:30:13 +0200 )edit

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


pierre-yves samyn gravatar imagepierre-yves samyn ( 2016-09-18 07:19:55 +0200 )edit

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.

floris v gravatar imagefloris v ( 2016-09-18 10:29:22 +0200 )edit

@pierre-yves samyn, @floris v: 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 gravatar imageLupp ( 2016-09-18 13:17:19 +0200 )edit

@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.

pierre-yves samyn gravatar imagepierre-yves samyn ( 2016-09-18 14:22:46 +0200 )edit

@pierre-yves samyn: (1.)Thanks for answering! (2.) I did not understand "this one" in your comment above.

Lupp gravatar imageLupp ( 2016-09-18 14:58:37 +0200 )edit

answered 2016-09-16 20:35:54 +0200

Lupp gravatar image

updated 2016-09-16 20:37:02 +0200

(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.

edit flag offensive delete link more


Great answer. Pity I cannot upvote you just yet... :)

CaveMan gravatar imageCaveMan ( 2016-09-17 14:53:43 +0200 )edit

Question Tools

1 follower


Asked: 2016-09-16 17:57:39 +0200

Seen: 1,628 times

Last updated: Sep 17 '16