Consulting

Results 1 to 7 of 7

Thread: Tricky Apostrophe problem

  1. #1

    Tricky Apostrophe problem

    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

  2. #2
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Valid point. Thanks.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.").
    Last edited by macropod; 09-03-2017 at 11:45 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    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.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7

    Thumbs up

    Ha ha.. Thanks. Marking this thread as solved.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •