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

edit retag close merge delete

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.

( 2016-09-16 18:38:52 +0100 )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...

( 2016-09-16 20:05:53 +0100 )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.

( 2016-09-16 20:23:25 +0100 )edit

Sort by » oldest newest most voted

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. Regards more ## Comments Awesome hint. ( 2016-09-17 08:59:59 +0100 )edit 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. ( 2016-09-17 16:30:13 +0100 )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 Regards ( 2016-09-18 07:19:55 +0100 )edit 1 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. ( 2016-09-18 10:29:22 +0100 )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? ( 2016-09-18 13:17:19 +0100 )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. ( 2016-09-18 14:22:46 +0100 )edit @pierre-yves samyn: (1.)Thanks for answering! (2.) I did not understand "this one" in your comment above. ( 2016-09-18 14:58:37 +0100 )edit (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.

more