2

I'm editing a book in LaTeX and its quotation marks syntax is different from the simple " characters. So I want to convert "quoted text here" to ``quoted text here''.

I have 50 text files with lots of quotations inside. I tried to write a regular expression to substitute the first " with `` and the second " with '', but I failed. I searched on internet and asked some friends, but I had no success at all. The closest thing I got to replace the first quotation mark is

s/"[a-z]/``/g

but this is clearly wrong, since

"quoted text here"

will become

``uoted text here" 

How can I solve my problem?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Marcus Nunes
  • 851
  • 1
  • 18
  • 33
  • I'm a little confused by your approach. Shouldn't it be the other way round with `s/``/"[a-z]/g`? But then, I think it'll be better with: `s/\`\`(.*?)''/"\1"/g` – Jerry Mar 04 '14 at 13:43
  • @Jerry Convertion is from ".." to ``.." – Abhishek Bansal Mar 04 '14 at 13:46
  • I think the reason some people (including me) got confused is because you didn't use inline code formatting (backticks) in the question. In a monospaced font it's easy to see that the final "quote" is really two apostrophes (`''`), but all I saw was `"`. – Alan Moore Mar 24 '15 at 20:19
  • possible duplicate of [Replacing quotation marks with "\`\`" and "''"](http://stackoverflow.com/questions/8967033/replacing-quotation-marks-with-and) – Sir Cornflakes May 29 '15 at 13:38

6 Answers6

3

I'm a little confused by your approach. Shouldn't it be the other way round with s/``/"[a-z]/g? But then, I think it'll be better with:

s/``(.*?)''/"\1"/g

(.*?) captures what's between `` and ''.

\1 contains this capture.


If it's the opposite that you're looking for (i.e. I wrongly interpreted your question), then I would suggest this:

s/"(.*?)"/``\1''/g

Which works on the same principles as the previous regex.

Jerry
  • 70,495
  • 13
  • 100
  • 144
2

Use the following to tackle multiple quotations, replacing all " in one step.

echo '"Quote" she said, "again."' | sed "s/\"\([^\"]*\)\"/\`\`\1''/g"

The [^\"]* avoids the need for ungreedy matching, which does not seem possible in sed.

Community
  • 1
  • 1
Richard Kiefer
  • 1,814
  • 2
  • 23
  • 42
  • Be careful when dealing with escaped `"` or other occurences of `"` which are not meant to be quotation marks, e.g. as in `"Quote" she said na\"ively, "again."`. These are party poopers. – Richard Kiefer Mar 24 '15 at 19:42
2

If you are using the TeXmaker software, you could use a regular expression with the Replace command (CTRL+R), and put the following into the Find field:

"([^}]*)"

and into the Replace field:

``$1''

And then just press the Replace All button. But after that, you still have to check that everything is fine, and maybe you need to do some corrections. This has worked pretty well for me.

0

Try grouping the word:

sed 's/"\([a-z]\)/``\1/'

On my PC:

abhishekm71@PC:~$ echo \"hello\" | sed 's/"\([a-z]\)/``\1/'
``hello"
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
0

It depends a little on your input file (are quotes always paired, or can there be ommissions?). I suggest the following robust approach:

sed 's/"\([0-9a-zA-Z]\)/``\1/g'
sed "s/\([0-9a-zA-Z]\)\"/\1\'\'/g"

Assumption: An opening quotation mark is always immediately followed by a letter or digit, a closing quotation mark is preceeded by one. Quotations can span over several words an even several input lines (some of the other solutions don't work when this happens).

Note that I also replace the closing quotation mark: Depending on the fonts you use the double quotation mark can be typeset as neutral straight quotation mark.

Sir Cornflakes
  • 675
  • 13
  • 26
0

You are looking for something contained in straight quotation marks not containing a quotation mark, so the best regex is "([^"]*?)". Replace it with ``\1''. In Perl this can be simplified to s/"([^"]*?)"/``\1''/g. I would be very careful with this approach, it only works if all opening quotation marks have matching closing ones, for example in "one" two "three" four. But it will fail in "one" t"wo "three" four producing ``one'' t``wo ''three".