![]() |
#1 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 202
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? |
![]() |
![]() |
![]() |
#2 |
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 30,912
Karma: 60358908
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. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Ex-Helpdesk Junkie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 19,421
Karma: 85400180
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>''''
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) 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. |
![]() |
![]() |
![]() |
#4 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 202
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. |
![]() |
![]() |
![]() |
|
![]() |
||||
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 |