Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Sigil > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 07-20-2015, 09:17 AM   #1
CalibUser
Addict
CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.
 
Posts: 201
Karma: 62362
Join Date: Jul 2015
Device: Sony
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?
CalibUser is offline   Reply With Quote
Old 07-20-2015, 10:39 AM   #2
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
Posts: 29,818
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
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.
theducks is offline   Reply With Quote
Old 07-20-2015, 01:35 PM   #3
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
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.

Last edited by eschwartz; 07-20-2015 at 01:46 PM.
eschwartz is offline   Reply With Quote
Old 07-20-2015, 02:14 PM   #4
CalibUser
Addict
CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.CalibUser goes to eleven.
 
Posts: 201
Karma: 62362
Join Date: Jul 2015
Device: Sony
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.
CalibUser is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions Help lauralein Library Management 1 11-12-2013 07:05 PM
Regular Expressions help deamonfruba Library Management 2 06-02-2012 02:09 AM
problem with Quote marks gwe Conversion 0 01-14-2012 07:21 PM
Regular Expressions geormes Calibre 4 08-04-2011 07:09 AM
Quote marks not formatting in .TXT to .EPUB? Sassyinkpen Calibre 11 10-07-2009 09:27 PM


All times are GMT -4. The time now is 12:15 AM.


MobileRead.com is a privately owned, operated and funded community.