I’m doing some text processing with Ruby.
For some text I’m working with: single quotes should never be outside of double quotes. So, I’d like to craft a RegEx which matches single-quoted strings, but not those enclosed in double quotes already, so I can swap them with a script. Make sense?
Thus, in the following examples, sentences #1, 2, 4, 6 and 8 are OK, while sentences #3, 5, and 7 contain incorrectly nested single quotes, which I’d like to swap:
- This is a sentence.
- This is a sentence “with double quotes.”
- This is a sentence ‘with single quotes.’
- This is a sentence “with a ‘nested single quote.’ Sometimes there are ‘more than one.'”
- This is a sentence ‘with a “nested double quote.” Sometimes there are “more than one.”‘
- This is a sentence “without a double ‘closing quote,’ which is common in this text.
- This is a sentence ‘without a single “closing quote,” common too, unfortunately.
- I don’t want to match apostrophes, however. That won’t work.
(bold face indicates the matches I’d like to make with the RegEx, so I can swap quotes.)
The point: I am trying to quote extended passages which already have quotes within them. This requires me to swap their doubles with singles.
Is this possible? I’ve been trying for hours, and I can’t seem to get it. Any help appreciated.
I don’t think regular expressions are the way to go for this one. Why not just scan through the text yourself?
(pseudocode)
I foresee future issues with this.