Page 1 of 1

[Solved] Replace expression with partial italics

Posted: Mon Nov 11, 2024 10:26 am
by gthcd
Hello,

Some help needed here please.

In a document I need to replace the string "abc" (no formatting) with the string "abc def" but I need the "def" bit – and only the "def" bit – in italics. Is there an expression in the Replace With box of Find & Replace that achieves this ?

Thanks in advance.

Re: Replace expression with partial italics

Posted: Mon Nov 11, 2024 11:19 am
by Hagar Delest
There may be more optimized solution but I would do that in 2 steps. Some power users with regular expressions may have a better solution in a single operation.
First find all occurrences of your word and replace it with the desired string plus a tag that will allow you to find in a second step all occurrences to be changed into italics.

For first step: in the find and replace dialog, find \b(abc)\b and replace by & xxxdefxxx, activating regular expressions. Then Replace All.
The \b regex is to take words only, in case the abc string could appear inside words too.
For the second step: find xxxdefxxx, then Find All (they will be all selected), then apply the italics (or a character style that applies italics), then in the replace field, put def and Replace All.

Note: tested with LibreOffice. But no reason it should not work with AOO too.

Please add [Solved] at the beginning of the title in your first post (top of the topic) with the πŸ–‰ button if your issue has been fixed.

Re: Replace expression with partial italics

Posted: Mon Nov 11, 2024 7:45 pm
by gthcd
Hello,

Thanks for the suggestions. The 2-step solution works, obviously (I should have thought of it !)

Regards.

Re: [Solved] Replace expression with partial italics

Posted: Tue Nov 12, 2024 6:14 am
by MrProgrammer
gthcd wrote: ↑Mon Nov 11, 2024 10:26 am I need to replace the string "abc" (no formatting) with the string "abc def" but I need the "def" bit – and only the "def" bit – in italics.
If abc is followed by a space you can do this in one step. In the following text ␒ represents a space.

Edit β†’ Find & Replace β†’ More options, select Regular Expressions, Search for (?<=abc)␒, Replace with ␒def␒ β†’ Format β†’ Typeface β†’ Italic β†’ OK, Replace All. I recommend unchecking Regular expressions, before clicking Close. I successfully tested this idea in OpenOffice 4.1.7.

(?<=abc)␒ uses a lookbehind assertion. It matches a space preceeded by abc, but only matches the space. The Replace All changes those spaces to space def space, all in italics, but you won't notice that those spaces are italicized.

It would be nice to use regular expression (?<=abc)(?=.*?). As before, this pattern looks for something after abc. (?=.*?) is a lookahead assertion. It finds, but does not match, a zero-length string after abc. Then we could replace the zero-length string between the assertions with space def space, italicized. However it seems that in OpenOffice's Find and Replace feature zero-length strings cannot be matched.