Search and replace by underscript

Hi,
I’d like search and replace T1 with T <undersctript 1> in my document. That is applying an underscript to the 1 not to the T.

Version: 24.2.1.2 (X86_64) / LibreOffice Community
Build ID: 420(Build:2)
CPU threads: 4; OS: Linux 6.7; UI render: default; VCL: gtk3
Locale: fr-FR (fr_FR.UTF-8); UI: en-US
24.2.1-3
Calc: threaded

Modify your question (click on then on the “pencil” icon – don’t answer with a comment so that contributors don’t need to scroll) to add OS name, LO version and save format.

Note the difficulty to apply automatically the change to all occurrences because you search on a “long” sequence (letter T, number 1 and probably some delimiter to avoid “false positive” matches) and want to apply a modification only on a “short” part of the match.

From your question wording, I fear you think of applying “directly” the underscript. You sould start by designing a character style containing all the desired settings for the final result. Search for the relevant occurrences and apply manually this character style on the to-be-subscript.

The advantage of apply a character style shows up immediately if you are not satisfied by the first attempt. You just modify the character style and all occurrences are immediately updated.

Of course, this works only if you save .odt because .doc(x) has no notion of character styles.

May I answer to my own question ?
Since it is easy to replace with a style applied to the whole match, I did it in two steps:
1 Search and replace T1 by, say, T___1___ (using a pattern not to be found eleswhere)
2 Search and replace ___1___ by 1 with the subscript ticked (via format button in the search/replace window)

You even have a better solution because yours causes direct formatting on your text (direct application of subscript which prevents further centralised tuning).

Your step 2 becomes:

  1. Find: ___1___, Replace: 1 (same as in your solution), no Format nor Attributes to avoid applying direct formatting; press Find All
  2. with the dialog still open, apply your preferred character style (previously configured) from the side style pane with a double-click on the name
  3. back in the dialog, press Replace All to eliminate the search sentinels

Another approach
@dontgiveup
If you haven’t applied any character style to the number 1 in T1 you could do this:

  • create a character style subscript
  • write T1 and apply the subscript character style to the number 1
  • cut/copy the modified text (T1) into the clipboard
  • SEARCH&REPLACE: search all T1 text in your document - every T1 is selected
  • leave SEARCH&REPLACE
  • *)paste CTRL+V the modified text (T1) from the clipboard into entire text
  • each selected T1 text fragment is replaced

See EDIT
This works on text fragments T1 and hard formatted T1, but not on character style formatted T1.
/See EDIT
.
EDIT
*) To avoid problems you could apply CTRL+M and then character style “No Character Style” to the “every T1 is selected” and then paste the modified text.

1 Like

Thanks to both @ajlittoz and @Grantler.
I wasn’t used to do multiple selections.
I’m still looking for a more straightforward way to do that but I’m afraid it’s not possible.

Easy to solve using regular expressions:

  1. Preferably, create a character style that has underscript.
  2. Open the Find and Replace dialog (the default shortcut is Ctrl+H).
  3. Check the Regular Expression Option under ‘Other options’.
  4. Type the following regular expression in the ‘Find’ textbox (See explanation bellow!):
    (?<=\b[[:upper:]])(\d+)\b
  5. Click the Find All button.
  6. Now that all the targetted numbers are selected, apply the style you created in step 1, or apply direct formatting (not recommended!)

Done!

Explanation for the regular expression:

I wrote the expression so that it will be able to match any number of digits for any uppercase letter. It has two parts:

the look-behind part:
(<=\b[[:upper:]])
Whatever is going to be matched must be preceded by an uppercase letter with nothing but punctuation or white space before it.

and the matching part:
(\d+)\b
Matches all ocurrences of numbers of any size followed by nothing but space or punctuation.

Check out ICU Regular Expressions if you wish to know more.