in reply to using substitution and pattern matching

One way is
s/'(\w+)'/"$1"/g;
Boris

Replies are listed 'Best First'.
Re^2: using substitution and pattern matching
by sauoq (Abbot) on Dec 18, 2004 at 17:08 UTC

    Update: Or, I could read the problem statement better. Somehow on first reading I didn't get that he was only concerned about words within single quotes. My reply in all its mistaken glory is preserved below for posterity.


    There are several problems with this answer.

    First, your \w+ only matches word characters. It fails to match several important things that might be found within quotations... like space for instance. And punctuation.

    Second, and less easy to fix, is the problem that a single quote used as an apostrophe might occur prior to the quoted text. For example, what if the text were the following?

    O'Reilly said, 'I did not do it!'
    Hold on though, it gets worse. What if the text were something like this...
    O'Reilly swore, 'I didn't! I swear I didn't! Honestly, I didn't swindle Miss O'Keefe!'

    Ugh.

    In a case like this, it's really important to figure out what assumptions you can rely on. For instance, if we could assume that a single-quote which needs to be replaced will always be either preceded or followed by whitespace and that apostrophes will always be nestled between non-whitespace characters then our task would become exceedingly simple. That might be a bad assumption though; something like "this ol' thing?" might be present. Maybe we can assume that an ending quote mark is always preceded by punctuation and followed by whitespace. Maybe we can't.

    Bottom line on this poster's problem is that it can't actually be done unless some assumptions can be made and there is a way to truly differentiate an apostrophe from an opening or closing single quote mark.

    -sauoq
    "My two cents aren't worth a dime.";