PDA

View Full Version : [SOLVED:] Tricky Apostrophe problem



Programmer_n
09-03-2017, 08:28 PM
My task: Trimming extra spaces to one space around apostrophe.

case 1: Jones'car. expected result: Jones' car
case 2: Jones 'car expected result: Jones' car
case 3: Sam' s car expected result: Sam's car
case 4: Sam 's car expected result: Sam's car

other cases like Sam ' s car or Jones ' car which should ideally return in Sam's car or Jones' car


I am experimenting with the code below by adjusting the spaces within the "" "" ; what if more than 1 space occur around apostrophe?

Is the below approach the right one to crack this problem?


sub Apostrophe()
ActiveDocument.Range.Text = Replace(ActiveDocument.Range.Text, "" & ChrW(&H2019) & "", "" & ChrW(&H2019) & " ")
end sub

gmayor
09-03-2017, 10:00 PM
See http://www.gmayor.com/replace_using_wildcards.htm
It is not possible to do this in one pass, and not reliable to do so in several (change 'car' for 'scooter' and you may see the problem).
I think I would be inclined to use the replace function to simply find the apostrophe character and manually check each occurrence.

Programmer_n
09-03-2017, 10:35 PM
Valid point. Thanks.

macropod
09-03-2017, 10:56 PM
You might start with a series of wildcard Find/Replace expressions like:
Find = ([!s])([ ^s]){1,}('s)
Replace = \1\3\2
Find = (s)([ ^s]){1,}(')([!s])
Replace = \1\3\2\4
Find = ([!s])(')([ ^s]){1,}(s)
Replace = \1\2\4\3
Find = (s)(')([!s])
Replace = \1\2 \3

However, there is no way for a F/R to tell whether a string like "Jones'scar" should be "Jones' scar" or "Jones's car". There is also the possibility a string like " 's" has nothing to do with possession (e.g. the ' begins or ends an expression contained in single quotes, like "he says 'you can bet on that' so often it annoys me.").

Programmer_n
09-03-2017, 11:33 PM
Well put. Now, I think it is futile to pursue this task. Thanks for pointing out.

Would it be possible to highlight the words containing 's and s' on yellow color so that I can manually go after the possessive words?

I tried pasting "(<[A-Z][a-z]{1,})'s>" in F&R and yellow highlight in replace option,but, unable to sense possessive words.

macropod
09-03-2017, 11:44 PM
In reality, all you need do is an ordinary Find for ' then check what's highlighted in either the Navigation pane or the 'Highlight all' Find Reading Highlight option. You can then check whether the ' is where it should be.

Programmer_n
09-04-2017, 01:58 AM
Ha ha.. Thanks. Marking this thread as solved.