MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Help with quote marks in regular expressions (https://www.mobileread.com/forums/showthread.php?t=263079)

CalibUser 07-20-2015 10:17 AM

Help with quote marks in regular expressions
 
Hi,

I am writing a plugin that needs to remove a space following the quote mark in the expression:

<p>'

I have looked on the web for solutions. One solution was to use triple quote marks so I tried:

html = re.sub(r'''<p>' ''', r'''<p>'''', html)

but this produced the error: EOL while scanning string literal.

I tried escaping the single quote mark in the replacement expression but that also produced an error.

Another web solution suggested placing the regular expressions in double quotes, so I tried the following and it worked:

html = re.sub(r"<p>' ", r"<p>'", html)

My question is: Suppose I have a replacement expression that contains both single and double quote marks. How could I write a replacement expression that includes a mixture of both?

theducks 07-20-2015 11:39 AM

This is the Sigil forum,
I am not sure Calibre query syntax works here in all cases.

BTW If you are working on a Sigil PI, I can move this thread. Just ask.

eschwartz 07-20-2015 02:35 PM

Code:

r'''<p>''''
Four single-quotes in a row -- the first three terminated the string literal, the last one began a new string literal.
You would need to escape it, but as you may have noticed, the backslash stays in the string. This only happens because you are using a raw string literal -- using
Code:

html = re.sub('''<p>' ''', '''<p>\'''', html)
works as expected.
Raw string literals are useful for strings in which you expect to have literal backslashes. That does not apply here.

Double-quotes work too, of course. :)

If you need to use both single- and double-quotes, make sure to escape whatever your delimiter is.

CalibUser 07-20-2015 03:14 PM

theducks - thanks for your response. The code that I put in my last post was not developed from Calibre (as my user name may suggest - I am going returning to Sigil for editing ePub books since the Sigil is continuing to be developed); I am learning Python for making Sigil plugins and could not find a way to put single and double quotes into the replacement expression and I wondered how this can be achieved.

eschwartz - thanks for providing a solution. I am new to Python so I am trying to learn how to use it. I will try your solution later.


All times are GMT -4. The time now is 08:30 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.