PDA

View Full Version : [SOLVED:] Tricky spacing problem



Programmer_n
09-06-2017, 07:04 AM
This task is about spacing around punctuation

For example:

The code below adds a spacing next to the period.

sentence :This is a sample sentence .This sentence is alright.
sentence :This is a sample sentence. This sentence is alright.


ActiveDocument.Range.Find.Execute FindText:="([A-Za-z]\.)([A-Za-z])", _
MatchWildcards:=True, Wrap:=wdFindContinue, Replacewith:="\1 \2", _
Replace:=wdReplaceAll

Case 1:

What if the period is attached to the next word.

sentence :This is a sample sentence .This sentence is alright.
expected result: This is a sample sentence. This sentence is alright.

Case 2:

What if there needs to be spaces on either side of the period.

This is a sample sentence.This sentence is alright.
This is a sample sentence . This sentence is alright.

Case 3: what if i need to trim spaces on either side.

This is a sample - sentence.
This is a sample-sentence.

Please ignore abbreviations or units that contain period and let's assume only end sentence period is present around the document.

Please help.

macropod
09-06-2017, 07:49 AM
You should be able to fix Case 1 (and a bunch more like it), plus the 2nd form of Case 2, with a wildcard Find/Replace, where:
Find = ([ ^s])([.,:;\!\?])
Replace = \2\1
Note that this would leave an extra space after the period for the 2nd form of Case 2; you could clean those up with another F/R.

Similarly, for the first form of Case 2:
Find = ([a-z][.,:;\!\?])([A-Z)
Replace = \1 \2

I don't know what you expect from Case 3 - not every hyphen indicates a hyphenated word.

Programmer_n
09-06-2017, 04:08 PM
Thanks for the reply.

Case 3 is to trim spaces around Emdash.

macropod
09-06-2017, 04:31 PM
Well, I'm sure you could figure out how to do that with a couple or ordinary F/R expressions...

Programmer_n
09-08-2017, 04:35 AM
Well, I'm sure you could figure out how to do that with a couple or ordinary F/R expressions...

^+
^+

solves it.