Page 1 of 1

[Solved] Find and replace space and paragraph characters

Posted: Tue May 21, 2019 7:44 am
by HaRo
I lost my MS Office in disk mishap. I'm trying OO. A problem. I frequently collect a block of words separated by 4 or 5 spaces.

1. Using MS Word, I find double space (put cursor into find box and press space bar twice) and replace with single space (put cursor into replace box and press space bar once.) Replace all. Repeat till words are only separated by a single space.
2. Then I convert to a list. Find space (put cursor into find box and press space bar once) and replace with paragraph (put cursor into replace box and type ^p).
3. Then sort into alphabetical order.
4. Change list to block. Find paragraph mark (put cursor into find box and type ^p) and replace with five spaces (put cursor into replace box and press space bar five times.)

How do I do the equivalent thing with OO? I can do the sorting, but not the rest of it.

Re: find and replace space and paragraph characters

Posted: Tue May 21, 2019 9:12 am
by RoryOF
Your step 1 is OK
Step 2, Find a space, Replace \n and ensure "More options" is set to "Regular Expressions"
Step 3 should be OK
Step 4, Find $, Replace a space, "More options" set to "Regular Expressions"

Above sequences just now tested and working correctly.

Re: Find and replace space and paragraph characters

Posted: Wed May 22, 2019 8:22 am
by HaRo
Thanks, but step 1 didn't work. I set up a block of words with varying numbers of spaces between them. Put five spaces into find box, one into replace box. It replaced all the fives with one. Then I put two into Find box, left one in replace box. Got "search key not found", even though there were plenty of places with multiple spaces. Tried with four spaces (since there were some words separated only by four spaces) and the same thing happened.

Re: Find and replace space and paragraph characters

Posted: Wed May 22, 2019 9:12 am
by RoryOF
If I am faced with multiple spaces I normally Find two spaces, replace with one space (no need for Regular experessions option in this case) and run that search repeatedly until "not found". Try again with "Regular Expressions" not checked

I am not driving your computer: as I wrote, my sequence had been tested with the Find and Replace specifiers I noted.
 Edit: If you are pasting from Internet sources, it is possible (indeed, likely) that what you are seeing as spaces (ASCII character 020H) are "non breaking spaces" (ASCII character 0A0H). Find and Replace does not treat these as equivalent. To disclose them, turn on /View /non printing characters; such non breaking spaces will show as shaded spaces. If so, select one of these, Copy it, then Paste into Find box. Replace with space, press Replace all button. Now Find should continue to find and replace ordinary spaces when you instruct it to do so, 

Re: Find and replace space and paragraph characters

Posted: Wed May 22, 2019 1:25 pm
by Lupp
There are many ways to fine-tune RegEx for such tasks. RegEx are much more powerful than the means MS Word offers (afaik).

Code: Select all

\s
e.g. will accept any kind of whitespace, including tab marks and hard line breaks. Consciously used together with with a quantifier like

Code: Select all

{2,}
with an ordinary space in "Replace with", and with the option "Current selection only" it may help in the present case.
See the attached example. It also contains a bit of additional information.

===Editing; for completeness only===
Paragraph breaks are not accepted by any RegEx in OpenOffice F&R 'Search for' except:
-1- denoted by the special character "$" as a special kind of "zero-length lookahead assertion".
-2- by "$" again like actual characters if a sequence of empty paragraphs shall be addressed.
The whitespace characters accepted by "\s" do not include paragraph breaks.

Re: Find and replace space and paragraph characters

Posted: Wed May 22, 2019 2:07 pm
by RoryOF
I usually use multiple simple F&R operations, Lupp. Note that your formula, given in the body of your reply, can affect some page style changes, so caution is suggested.
 Edit: I used a copy of an existing document (A5 pages) inserted into a blank Writer document, and failed to notice that the Default style of the blank document was retained (A4 page) rather thsn the Default style (A5 page) used in the inserted document. 

Re: Find and replace space and paragraph characters

Posted: Wed May 22, 2019 4:03 pm
by erbsenzahl
@HaRo:
Your Swiss knife could be the AltSearch.oxt extension.

When downloaded install it via Tools menu > Extension Manager and start OpenOffice anew.

To replace spaces into paragraphs and return use for paragraph only \p. That's all. As simple as in Word.

Re: Find and replace space and paragraph characters

Posted: Wed May 22, 2019 4:37 pm
by RoryOF
A possible problem users may find with AltSearch is that compared with OO's F&R it takes a long time in any document of size (hundreds of pages).

Re: Find and replace space and paragraph characters

Posted: Thu May 23, 2019 5:07 am
by HaRo
Thanks, erbsenzahl. I'll give it a try.

Re: Find and replace space and paragraph characters

Posted: Thu May 23, 2019 6:07 am
by HaRo
Nope. The first part works fine, both for English and Chinese. I set up a block of words with varying spaces between them, use space bar to put double spaces in find, single in replace, and replace all. Repeat until there is only a single space between them.
But when I put space in find, and \p into replace, it refuses to do anything. I've tried it with end-of-word, any-space, and non-breaking-space from the list. It won't find them, won't replace them.

Re: Find and replace space and paragraph characters

Posted: Thu May 23, 2019 6:47 am
by RusselB
The following was done using the default OpenOffice Find & Replace dialog in Writer.
This has been briefly tested.
In the Find & Replace dialog put \s+ in the Find section and \n in the Replace section.
Select More Options and ensure the Regular expressions is checked.
My test shows that this finds multiple occurrences of the space character and then replaces them with a single paragraph character.

Re: Find and replace space and paragraph characters

Posted: Thu May 23, 2019 7:16 am
by HaRo
By George, you've got it!

Thanks, RusselB.

On my quick tests, that will do what I want both for English and Chinese!

I can replace multiple places with a single para mark, sort (OO doesn't automatically recognise Chinese, but gives more sorting options for Chinese than MSWord) and the replace the para marks with multiple spaces.

I'll come back and moan some more if I have problems with it, but right now it looks fine.

Re: Find and replace space and paragraph characters

Posted: Thu May 23, 2019 8:36 am
by RoryOF
HaRo wrote:Nope. The first part works fine, both for English and Chinese. I set up a block of words with varying spaces between them, use space bar to put double spaces in find, single in replace, and replace all. Repeat until there is only a single space between them.
But when I put space in find, and \p into replace, it refuses to do anything. I've tried it with end-of-word, any-space, and non-breaking-space from the list. It won't find them, won't replace them.
When using the \n in either Find or Replace one must use More Options: Regular expressions.