[Solved] Replace expression with partial italics

Discuss the word processor
Post Reply
gthcd
Posts: 10
Joined: Fri May 08, 2020 4:57 pm

[Solved] Replace expression with partial italics

Post 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.
Last edited by Hagar Delest on Mon Nov 11, 2024 8:00 pm, edited 1 time in total.
Reason: tagged solved.
Open Office 4.1.10 on MAC OSX 10.13.6
User avatar
Hagar Delest
Moderator
Posts: 32850
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Replace expression with partial italics

Post 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.
LibreOffice 24.8 on Xubuntu 24.10 and 24.8 portable on Windows 10
gthcd
Posts: 10
Joined: Fri May 08, 2020 4:57 pm

Re: Replace expression with partial italics

Post by gthcd »

Hello,

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

Regards.
Open Office 4.1.10 on MAC OSX 10.13.6
User avatar
MrProgrammer
Moderator
Posts: 5097
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Solved] Replace expression with partial italics

Post 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.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Post Reply