0

I have found a couple posts around the internet and on here on how to remove all but the first letter of every word in a paragraph (or the whole text) using regex and Microsoft Word, but nothing has seemed to work in LibreOffice Writer.

I have been trying to use the Find and Replace dialog and have regular expressions checked in the options.

As an example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Would become:

L i d s a, c a e, s d e t i u l e d m a.

Keeping punctuation and case sensitivity would be great. I also am not opposed to using a different method/program for this.

2
  • Both Flash's and Tim's solutions work, but while I have a fondness for regular expressions that replace matches with empty strings (Tim's answer), Flash's solution is seen to be more efficient: 114 versus 452 steps for the test string. Commented Aug 31, 2024 at 6:59
  • 2
    @CarySwoveland seems @mordock's own find is even better: \B[a-z]+ or \B\w+ would be 75 steps or 76 steps (not sure why the difference) Commented Aug 31, 2024 at 7:24

2 Answers 2

2

Assuming LibreOffice's regex support fixed width lookbehinds, you could match on the following pattern:

(?<=\w)\w

and then replace with empty string. This pattern will match any word character except those not preceded by word characters (e.g. at the start of a word).

Here is a running demo.p

Edit:

As suggested in the comments, we can optimize the pattern to:

(?<=\w)\w+

This will match entire sub-words in one go, reducing the number of replacement steps.

Sign up to request clarification or add additional context in comments.

3 Comments

libreoffice does support this. (?<=\w)\w+ might be more efficient.
Yes, this worked. There are a couple formatting issues with numbers and the like, but I have no problems fixing those by hand. Also, \B[a-z] seems to give the same results, which I had just figured out and was coming to update my question with that information. Thank you!
Further to @jhnc's suggestion, for the test string, it is seen that replacing \w with \w+ reduces the number of matches from 83 to 19 and the number of steps from 452 to 132.
2

Use find and replace and check the regex (Regular expressions) checkbox.

Search for: \b(\w)\w+, replace with: $1

  • \b is a word boundary anchor that ensures the match starts at the beginning of a word.
  • (\w) captures the first letter of the word and makes it the first group
  • \w+ matches the rest of the word, which will be replaced.
  • $1 in the replace field inserts the first captured group (the first letter in our case).

2 Comments

This solution works for the first word of a paragraph, but unfortunately fails to change any words thereafter, unless it's a new paragraph.
Weird. Works in my case. But you could try (\b\w)\w* then.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.