1

I am attempting to format a series of test questions for implementation to an online course in LibreOffice Writer. The test question format was changed recently and I have to update all 500+ questions in a text file before implementation.

The questions currently look like this:

1. alter verb (awl-ter) 
to change; become different or modified
■Example: The design of the king’s suit was altered to suit his taste.
□Note: Can be confused with altar , alter. 

we need to change the format to look like this:

Word: alter verb (awl-ter) 
Definition: to change, become different or modified 
Example: The design of the king’s suit was altered to suit his taste. 
Possible Exam Question: Can be confused with altar , alter. 

I know enough regex to be able to format most of the lines automatically, with the exception of the definition line. I specifically need to add the word "Definition:" to the beginning of the line immediately after the "Word" line, but I cannot seem to figure out how to do this properly.

I have tried using (\b\d+\.\s\w.*[\r\n]) which can find the first line of each definition group (as shown above), and up to the new line / return - however, when attempting to append the text using $1Definition: which according to Writer's regex help file should keep the original findings and append whatever is after the "$1", it will either replace once and never work again with subsequent "replace" clicks, or will replace everything with a literal string of $1Definition: if using the "replace all" button...

I've added a screencap of the search function highlighting the string. The search does seem to find what I'm looking for and will highlight the first line of each set when clicking "find all".

The problem comes to fruition (as mentioned above) when attempting to click "replace" more than once or click "replace all".

I'm currently at a loss as to what the problem is / how to fix this. It almost seems like my Regex is somehow corrupting the search query and output.

John
  • 13
  • 5
  • Does the find and replace have to be in LibreOffice? Maybe if there is a lot of formatting I guess. I had a go in Writer and matching across lines does not seem straight forward. I was playing with `(^.*?)([.|\n]+■Example:)` and replacing with `Definition: $1$2` and looks to work in VSCode (as an example text/code editor available across Linux, Mac, Windows). Example (before the replace on the left, after on the right): https://ibb.co/SXGhC2p – MDR Apr 11 '20 at 07:39
  • @MDR - hmm... I was looking around on https://regex101.com and found that it has a replace function. I used your suggestion and tried it out with my original code and sure enough it works perfectly, meaning the problem is with LibreOffice's handling of regex replacements sadly. It will work as a work-around for now, i suppose, but at the cost of losing my formatting. Easily replaced though... – John Apr 11 '20 at 08:01
  • @MDR - seeing as how this seems to be an issue with how LibreOffice handles the regex, and not an actual problem with the coding, I would like to close this question (but give you credit for the answer). If you can write up a quick answer pointing that out and suggesting another editor / site / etc that has the replace feature I can mark that as the answer for this question, then I will send a bug report off to LibreOffice's forum. :-) – John Apr 11 '20 at 08:05
  • Maybe ask on their forum on how to do multiline regex matches (match across lines) or how to implement the regex provided here in Writer. If the regex works for other editors and regex101 then it would be interesting to know what change is required for it to work with Writer or if this is not possible is it deemed a bug or a known limitation of the program. Anyhow, answer posted. Glad it helped a bit. Good luck! – MDR Apr 11 '20 at 09:04

2 Answers2

0

As per the comments, writing this up as an answer.

For the given issue of how to change...

1. alter verb (awl-ter) 
to change; become different or modified
■Example: The design of the king’s suit was altered to suit his taste.
□Note: Can be confused with altar , alter. 

To (add 'Definition: ' to the second line):

1. alter verb (awl-ter) 
Definition: to change; become different or modified
■Example: The design of the king’s suit was altered to suit his taste.
□Note: Can be confused with altar , alter. 

...whilst LibreOffice Writer's regex does not seem to like multiline matches, you could use another editor like VSCode...

enter image description here

Or Brackets (Google Brackets.io)...

Before replace:

enter image description here

After:

enter image description here

If you decide to use another editor try the regex...

(^.*?)([.|\n]+■Example:)

And replace with...

Definition: $1$2

Hope it helps.

MDR
  • 2,610
  • 1
  • 8
  • 18
0

The way to do this with LibreOffice is the AltSearch extension. It is designed to handle regular expressions with line breaks and paragraph breaks.

Search for: (\b\d+\.\s\w.*)$
Replace: \1\nDefinition: 

Then press Replace All.

alt find and replace - replaced 3 times

When I tried it, Current selection only was checked by default, so you may need to uncheck that. Also, I found that $ worked, but \p can be used to match a paragraph break.

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • Thank you for your submission. This is definitely noteworthy and I will check it out. For clarification, the reason I marked MDR's solution as the "answer" is because it is what originally helped me resolve the overall issue (albeit, not within LO-Writer). – John Apr 23 '20 at 01:05