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…

9 thoughts on “LibreOffice RegEx Backreferences

  1. programming as an experimental science

    I got the same thing for Linux installation. As it turns out, there seems to be some cosmic law in effect: “If there are 5 nominal ways to accomplish something, 2 won’t work on your example, 1 is too horrible to contemplate, and the “best” approach isn’t clear until you’ve actually tried both of the others.” I drew the line at installing a built-in iso-writer on the Windows 7 box.

    I know more about usb sticks, nfs mounts (and firewall rules) and cdrecord quirks than I ever expected. Total monetary expenditures so far are $8.00 for an extra USB stick and 7 CD-R disks purchased a decade ago… Total calendar time, about 6 weeks.

    Now to verify that I got the right data on the CDs (and they pass an fsck), thence to install and learn a subversion client for some of the application software.

    1. the “best” approach isn’t clear until you’ve actually tried both of the others.

      It took a long time for me to realize that I didn’t have to get a Linux installation right on the first try: it was OK to iterate a few times, trying this and trying that, until I understood (more or less) what was going on.

      Then do it once more, with everything in order, and it’d be all good!

      Agreed, that’s a luxury applicable only to personal installations. If you’re an IT droid, then getting it right the first time, every time, is vital. In that situation, you get to do it a lot, so you’re never out of practice.

      1. I tell my employees to document every path they take and what happened, to build systems to throw away, and to refactor their notes into a stepwise procedure to exactly duplicate the results we approved; then to give those notes to the server team to build us the real thing. FWIW.

        1. give those notes to the server team to build us the real thing

          And they build one to verify that it works, before committing to The Real Build, right?

          I like the “refactor the notes” process, because it’s vital to sift through all the detail to extract the meaningful bits that you didn’t know were important until later. Part of what I’m documenting here are those details from my tinkering, because my timeline for some projects far exceeds my memory… but having the details available for later reference really helps.

        2. The first one built by the server team is the Real Thing — but if it didn’t work as described, at least it’s another chance to get the notes right.

          I like using a wiki for documentation because it makes it so easy to capture notes as you work and refine them as you iterate and because it preserves the history of changes for later study. I haven’t quite converged on the right format to produce step-by-step procedures and still retain the history of dead ends and believed-to-be wrong turns, but I do like footnotes and links.

          If this comment is out of sequence, it’s because the comment template doesn’t allow replies past a certain depth so I’m replying to my own comment rather than to yours.

          1. The first one built by the server team is the Real Thing

            The Force is strong with your team!

            the comment template doesn’t allow replies past a certain depth

            I set the depth limit to 4, because the theme squishes anything deeper into absurdity… [sigh]

      2. I’m taking advantage of the install-many-times approach. Had some delays getting X to start right with XFCE, so I tried KDE to get some of the bits and pieces understood. (I’ve used KDE on a P2 box.)

        The RH-7 install pretty much did everything, and made sure you knew it was doing it. The Slackware seems to be doing the same stuff, but a lot of it is occurring automagically. It’s going to take a little getting used to.

  2. This is how Perl regexps work—I think this dates all the way back to Henry Spencer’s implementation. Ah, I date myself..

    1. That’s the great thing about standards: everybody can have one!

      The fact that all the various regex dialects resemble bursts of line noise doesn’t help, either…

Comments are closed.