Find/Replace -- how to exclude certain words

Discuss the word processor
Post Reply
Straitsfan
Posts: 22
Joined: Mon Sep 29, 2008 11:31 pm

Find/Replace -- how to exclude certain words

Post by Straitsfan »

I was wondering if there was any way to exclude certain words in a find& replace search with wildcards. For example. I need to find instances of a period with less than or more than two spaces, but not include "Mr." or "Ms." "Mrs." et cetera, because they are followed by a single space. I would also, if possible like to know how to ignore a combination of a middle initial followed by a period, and/or other words that contain periods, such as "U.S." etc cetera. I'm only looking for a period at the end of a sentence, followed by less than/one space, or more than one space.

Why doesnt OO automatically put two spaces after a period, or even give the option, and allow exceptions to it? Very frustrating.
LO 5.2.3.3, OS X El Capitan
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Find/Replace -- how to exclude certain words

Post by FJCC »

Sorry, I can't think of a way to search only at the end of sentences. I will mess around some more but there are a lot of special cases to deal with.
I will note that most style manuals call for a single space after a period, at least according to this article and this blog and other sources on line.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Straitsfan
Posts: 22
Joined: Mon Sep 29, 2008 11:31 pm

Re: Find/Replace -- how to exclude certain words

Post by Straitsfan »

Wish it were so for my work, but they want to spaces after a period.
LO 5.2.3.3, OS X El Capitan
jrkrideau
Volunteer
Posts: 3816
Joined: Sun Dec 30, 2007 10:00 pm
Location: Kingston Ontario Canada

Re: Find/Replace -- how to exclude certain words

Post by jrkrideau »

Why doesnt OO automatically put two spaces after a period, or even give the option, and allow exceptions to it? Very frustrating.
Because, at least with proportional fonts, there is no need for it. Two spaces after a period is a leftover from the days of typewriters and mono-spaced type.

However, that said, try Tools > Autocorrect Options > Options > and see if Ignore double spaces is checked. If it is unchecked you can do two spaces after a period. I am not sure that you can specify exceptions.
LibreOffice 7.3.7. 2; Ubuntu 22.04
MPEcho
Posts: 99
Joined: Wed Sep 07, 2016 11:30 pm

Re: Find/Replace -- how to exclude certain words

Post by MPEcho »

Straitsfan wrote:Wish it were so for my work, but they want to spaces after a period.
You could try two steps. (now that I've been schooled on the correct regex syntax in another thread)
In the search and replace dialog check "regular expressions" in "other options. Then for any number of spaces after a period:

Code: Select all

\.([:space:])*
Replace with a period and two spaces ". " Note, not regex.
Then on your second pass, convert back the special cases. For example, this would match Mr., Ms., Mrs. and Messrs. followed by any number of spaces :

Code: Select all

M[ers]*\.([:space:])*
To search for a capital letter followed by a period and any number of spaces, check "Match case" in the dialog and use:

Code: Select all

[A-Z]\.([:space:])*
In each of those cases, you will then replace with one space, not two.

I'm not sure it is practical or even possible to formulate a regex that will work for every special case without knowing what is in the document. So this "solution" is very brute force. But using this strategy will vastly cut down on proofing. Trouble will still result. Such as making a mess of urls if you have them in the document. But you will have much less to correct, expecially knowing that you have an extra space after every initial.
Depending on the document size, it might be simpler to just convert everything to two spaces, using replace all, then manually click through and only replace with one where appropriate.
Libre Office 5.1.6.2 Ubuntu 16.04
Straitsfan
Posts: 22
Joined: Mon Sep 29, 2008 11:31 pm

Re: Find/Replace -- how to exclude certain words

Post by Straitsfan »

Found a partial solution at least:

([:alpha:]{2,}(^Mr|Ms))\.([:space:]{0,1}|[:space:]{3,})[:alnum:]

I think this succeeds in excluding Mr. or Ms. (or any other words in the parenthses), then finds the improper amount of spaces. I just need to understand how to replace the defective number of spaces. :-)
LO 5.2.3.3, OS X El Capitan
MPEcho
Posts: 99
Joined: Wed Sep 07, 2016 11:30 pm

Re: Find/Replace -- how to exclude certain words

Post by MPEcho »

Straitsfan wrote:I just need to understand how to replace the defective number of spaces. :-)
I have not tried your regex. If it finds and selects the "." and the wrong number of following spaces, just put ". " (The period and required number of spaces) in the "replace with" dialog. In my installation that does not need to be regex.
Libre Office 5.1.6.2 Ubuntu 16.04
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Find/Replace -- how to exclude certain words

Post by John_Ha »

Straitsfan wrote:I was wondering if there was any way to exclude certain words in a find& replace search with wildcards. For example. I need to find instances of a period with less than or more than two spaces, but not include "Mr." or "Ms." "Mrs." et cetera, because they are followed by a single space.
Instead of trying to do it in a big single step, do it in several small steps where you first change everything, and you then correct the errors you have introduced. For example, where sp = a space:

Step 1. Change all ".sp" to ".spsp "
Step 2. Change all "Mrs.spsp" to Mrs.sp"
Step 3. Change all "Mr.spsp" to Mr.sp"
etc
Place the commands in a macro run by clicking an icon on the toolbar. One click now does the entire document. Test your macro by running it ... and then running it again. Does the second run mess up the document?

I use a macro to do this where every change is a single line in the macro and I have about 30 lines. It is then trivially easy to add, remove or edit each simple change.

Note it is quite tricky as a sentence can end with ., !, ?, ", ' so you must search for all of these with a regex like ([.|!|?|"|'])( +) for any of the symbols followed by one or more spaces (in case you mistyped and already have two or three spaces). Now replace what you found with $1spsp. You probably also need to convert any non-breaking spaces following a stop to normal spaces.

See [Tutorial] How to record a macro (and Regular Expressions) for help on regular expressions.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
CatBookWriter
Posts: 25
Joined: Wed Jan 17, 2018 5:43 pm

Re: Find/Replace -- how to exclude certain words

Post by CatBookWriter »

Straitsfan wrote:Wish it were so for my work, but they want to spaces after a period.
Why does your employer require a grammatical error? Single spaces after periods is what people learn in English classes.
OpenOffice 4.1.5 on Windows 10
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Find/Replace -- how to exclude certain words

Post by RoryOF »

CatBookWriter wrote:
Why does your employer require a grammatical error? Single spaces after periods is what people learn in English classes.
With respect, this is not a grammatical error, but a typesetting convention; like many conventions it could be erroneous. For example consider USA date structure versus European date structure versus ISO standard date structure.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Find/Replace -- how to exclude certain words

Post by John_Ha »

CatBookWriter wrote:Why does your employer require a grammatical error? Single spaces after periods is what people learn in English classes.
It isn't an error - it's a choice of house style. The Guardian newspaper calls NASA Nasa, but calls IBM IBM. easyJet spells its name like that.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
Post Reply