Ask Your Question
1

insert paragraph break before numbers

asked 2018-03-06 20:44:19 +0200

catbill gravatar image

updated 2018-03-06 20:48:18 +0200

I have a list of questions and answers. All questions begin with a number and I would like to insert paragraph breaks before those numbers.

Using regular expressions, the numbers could be identified by ^[0-9] and for replacement, \n inserts a paragraph. However, I cannot figure out how to specify that the numbers should be retained.

I have looked over the LibreOffice and OpenOffice references and have not found the way to do this. It must not be hard. Perhaps it would be easier if I knew what this property is called.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2018-03-06 21:51:19 +0200

David gravatar image

updated 2018-03-06 21:55:29 +0200

What you want is a "back reference", and strangely, it is not covered in the LibO regex help, although it is picked up in the OpenOffice regex wiki. The notation for this has varied with different versions of LibO Writer (I think the regex "engine" changed at some point), and it is different from what one usually uses (and what is described in the Regular-Expressions.info link, above). I'm using v. 6.0.1.1, and this is what works for me:

  1. Ensure that you have the "[✓] Regular Expressions" option checked under the "Other options" drop down in the Find & Replace dialog (of course).
  2. For your Search string use: (^[0-9])
    The parentheses are important here, as they define the part of the search you're going to refer back to.
  3. For your Replace string use: \n$1
    This means:
    • \n = insert new line
    • $1 = insert the value between the first set of parentheses in the "Find" string

So you could see from that, if you had a "Find" of more than one element, e.g.:

(^Cat)(.*)(hat\.$)

and the "Replace" string of, e.g.:

Dog$2bog.

For the text:

Cat in the hat.

Catcher in the rye.

Only the first line would be changed to:

Dog in the bog.

with the $2 part picking out the "middle" regex string from "Find".

edit flag offensive delete link more

Comments

David, Thank you so much. I can't tell you how helpful this is. The documentation I have read about regular expressions appears to be written for people who already know how to use them. I had read about back references on the OpenOffice Regex wiki, but could not figure out what was being described. Your explanation is the clearest and most helpful that I have seen. Thanks again. Now I have a similar question but will ask it in another post.

catbill gravatar imagecatbill ( 2018-03-07 05:04:21 +0200 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2018-03-06 20:44:19 +0200

Seen: 126 times

Last updated: Mar 06 '18