Regex: Replacing single quotes but taking care of apostrophes

(I am certain that this question has already been asked, but I cannot find it.)

I need to replace anything between single quotes with \enquote{}, the capture to be put inside the curly brackets. With double quotes I can get away with:

  • FIND: "(.*?)"
  • REPLACE: \\enquote{$1}

But with single quotes there is the obvious problem of apostrophes. Is there way to solve that problem.

Example:

He said, 'it's always sunny here.' 
4

2 Answers

One multi-step approach:

  1. Use Search and Replace, "replace all" to replace space+' with
    something appropriate (e.g. space+")
  2. repeat .1 for '+space.
  3. you might also have '. to handle...
    (i.e. ' at the end of a sentence, and also at the end of a line -
    and how about at the very beginning of a line?)
  4. Now use Search and Replace to replace ' into " for
    the remaining instances and select to replace or not, manually (Yes/No).

To get this to work with an automated process?
That is a bit harder to say the least, I'm all ears regarding that...

If you can find a way to express the Search-items above with regex'es (i.e. $ man re_format) then you might be at least part of the way to automation.
This might be of some help there though, but I take it that \b isn't available in all regex-implementations.

As you didn't give the language/tool you are using, I give a solution that uses Notepad++.

  • Ctrl+H
  • Find what: \w'\w(*SKIP)(*FAIL)|'(.+?)'(?!\w)
  • Replace with: \\enquote{$1}
  • CHECK Wrap around
  • CHECK Regular expression
  • CHECK . matches newline*
  • Replace all

Explanation:

 \w # 1 word character ' # single quote (in fact an apostrophe) \w # 1 word character (*SKIP) # skip this match (*FAIL) # abort this match (don't considere the apostrophe) | # OR ' # single quote (.+?) # group 1, 1 or more any character, not greedy ' # single quote (?!\w) # only if not followed by a word character 

Screen capture (before):

enter image description here

Screen capture (after):

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like