2

In LibreOffice Writer, I want to use regex to search for all words that start with the letter c and to replace them with themselves and another word.

For the first part, I use the following and all is fine.

\bc[a-z]+

Yet for the replace part, when I write

& text

it replaces cat with & text instead of cat text.

What am I doing wrong?

2 Answers 2

4

The other answers almost got it right. The find expression is correct:

\b(c[a-z]*)\b

I've used * in place of + in order to match c as a single word, and I've added a trailing \b to make the word limits more clear, but the original form is OK.

The replacement string should be:

$1 text

This does accord with Jim K's documentation link, but it is rather confusing in this area.

Specifically, \1 can be used in the search expression, to repeat a string matched earlier in the expression, but $1 must be used in the replacement string. The use in the search string means that (a.b).*\1 will match a and b with a character between, followed by a and b with the same character between. This is not the same as a.b.*a.b, where the intervening characters may be different. The example in the documentation link is correct, but does nothing to explain where it will be useful.

As a final note, my version 5.1.4.2 of LibreOffice Writer refuses to make substitutions when the search string starts with \b, although it finds all the instances correctly; a trailing \b works as expected. This is a bug - OpenOffice 4.1.3 works correctly. The work-round is to use ([^a-z]\bc[a-z]*)\b as the search string with the same replacement string: this works in all cases except when the first word of the document begins with c.

5
  • Thank you for the detailed answer. I have tested your regex on both LibreOffice Writer 5.2.5 and 5.3, and it still replaces cat with $1 test, although it correctly finds all the instances. The only situation in which it works is when the c word is at the beginning of the line. Since this regex is undoubtedly the correct one, it seems that we are dealing with a bug.
    – Cezar B
    Feb 4, 2017 at 13:08
  • Since I've found a bug with the leading \b, maybe they still haven't got it right. Did you try my work-round?
    – AFH
    Feb 4, 2017 at 14:35
  • My 5.1.4.2 was on Ubuntu 16.4.1. I just tested 5.2.5.1 on Win10 and got the same result - the strings are found, but not replaced, when I use a leading \b, while the work-round still does the job.
    – AFH
    Feb 4, 2017 at 14:54
  • Thinking completely outside the box, I wonder if you have a Romanian keyboard and it's generating the alternative small dollar sign (U+FE69). Try copying and pasting the replacement string from my answer and see if that works properly. It's a wild guess, but it's the only explanation I can thing of. Note that the keyboard map for Writer is not necessarily the same as the normal system keyboard.
    – AFH
    Feb 4, 2017 at 15:09
  • Yes, the work-around did the job in LibreOffice Writer 5.3.0.3. I don't know what went wrong in the first few tries (maybe I typed an extra blank space), but it now works precisely as you mentioned. It replaces the words correctly. I appreciate all of your help. Thank you.
    – Cezar B
    Feb 4, 2017 at 15:50
1

The "find" expression is missing parentheses to capture the word.

\b(c[a-z]+)

The "replace" expression is correct as is.

Documentation: https://help.libreoffice.org/Common/List_of_Regular_Expressions

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .