Advertisements

Archive for April 3rd, 2013

LibreOffice RegEx Backreferences

I needed to replace all the ordinary spaces between numeric values and their units (as in 3.5 V) with non-breaking spaces. LibreOffice Writer implements regular expression searches, but their notion of marking and replacing references trips me up every time. This part of the Fine Manual describing how parenthesized targets work will come in handy again:

In the Search for box:

Defines the characters inside the parentheses as a reference. You can then refer to the first reference in the current expression with “\1”, to the second reference with “\2”, and so on.

For example, if your text contains the number 13487889 and you search using the regular expression (8)7\1\1, “8788” is found.

You can also use () to group terms, for example, “a(bc)?d” finds “ad” or “abcd”.

In the Replace with box:

Use $ (dollar) instead of \ (backslash) to replace references. Use $0 to replace the whole found string.

As nearly as I can tell, there is no escape sequence that denotes a non-breaking space, so I had to manually enter one using Shift+Ctrl+Spacebar, copy it, and paste it into the replacement text string.

The search-and-replace dialog looked like this:

LibreOffice RegEx Backreferences

LibreOffice RegEx Backreferences

Yes, you can search for strings inside parentheses, use parentheses to mark references, and then jam references in the replacement. This makes my head hurt every time: programming as an experimental science…

Advertisements

9 Comments