Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing a slash and apostrophe with Python regex
    text
    copied!<p>I am attempting to parse a Wikipedia SQL dump with the Python regular expressions library. The ultimate goal is to import this dump into PostgreSQL, but I know the apostrophes in strings need to be doubled, beforehand.</p> <p>Every apostrophe in a string in this dump is preceded by a backwards slash, though, and I'd rather not remove the backwards slashes.</p> <blockquote> <p>(42,'Thirty_Years\'_War',33,5,0,0)</p> </blockquote> <p>Using the command</p> <pre><code>re.match(".*?([\w]+?'[\w\s]+?).*?", line) </code></pre> <p>I cannot identify the apostrophe in the middle of 'Thirty_Years\'_War', when 'line' is parsed from a text file.</p> <p>For comparison, these lines work fine when parsed (sans the last line).</p> <blockquote> <p>The person's car</p> <p>The person's car's gasoline</p> <p>Hodges' Harbrace Handbook</p> <p>'Hodges' Harbrace Handbook'</p> <p>portspeople',1475,29,0,0),(42,'Thirty_Years\'_War',33,5,0,0)</p> </blockquote> <p>Correct and expected output (sans the last line):</p> <blockquote> <p>The person''s car</p> <p>The person''s car''s gasoline</p> <p>Hodges'' Harbrace Handbook</p> <p>('Hodges'' Harbrace Handbook')</p> <p>portspeople',1475,29,0,0),(42,'Thirty_Years\'_War',33,5,0,0)</p> </blockquote> <p>Using the command</p> <pre><code>re.match(".*?([\w\\]+?'[\w\s]+?).*?", line) </code></pre> <p>breaks it.</p> <blockquote> <p>The person''s car</p> <p>The person''''s car''''s gasoline</p> <p>Hodges'' Harbrace Handbook</p> <p>(''''''''Hodges'''''''' Harbrace Handbook'''''''')</p> <p>portspeople'''''''''''''''',1475,29,0,0),(42,''''''''''''''''Thirty_Years\''''''''''''''''_War'''''''''''''''',33,5,0,0)</p> </blockquote> <p>Is it stuck in some sort sort of loop? What is the correct regex code to use?</p> <p>I am not thinking about SQL injection attacks because this script is only going to be used for parsing dumps of Wikipedia articles (that don't contain examples of SQL injection attacks).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload