Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have three issues:</p> <ol> <li>You need to use <code>flags=re.IGNORECASE</code> and not just <code>re.IGNORECASE</code> in <a href="http://docs.python.org/2/library/re.html#re.sub" rel="nofollow">re.sub</a>. That is a keyword argument.</li> <li>You need to use <code>\b</code> to find the word boundary. </li> <li>You should not use <code>str</code> as a name since you will overwrite the built-in by the same name</li> </ol> <p>This works:</p> <pre><code>import re tgt='''\ CHAR(124) + (SELECT TOP 1 CAST(name AS VARCHAR(8000)) FROM (SELECT TOP 1 colid, name FROM [Projects]..[syscolumns] WHERE xtype=char(85) AND id = OBJECT_ID(NCHAR(69)+NCHAR(78)+NCHAR(95)+NCHAR(69)+NCHAR(109)+NCHAR(112)+NCHAR(108))''' pat=r'(\bn?char\((\d+)\)(?:\s*\+\s*)?)' def conv(m): return chr(int(m.group(2))) print re.sub(pat, conv, tgt, flags=re.IGNORECASE) </code></pre> <p>More completely:</p> <pre><code>import re tgt='''\ CHAR(124) + (SELECT TOP 1 CAST(name AS VARCHAR(8000)) FROM (SELECT TOP 1 colid, name FROM [Projects]..[syscolumns] WHERE xtype=char(85) AND id = OBJECT_ID(NCHAR(69)+NCHAR(78)+NCHAR(95)+NCHAR(69)+NCHAR(109)+NCHAR(112)+NCHAR(108))''' pat=r'(\bn?char\((\d+)\)(?:\s*\+\s*)?)' def conv(m): return chr(int(m.group(2))) print re.sub(r''' ( # group 1 \b # word boundary n?char # nchar or char \( # literal left paren (\s*\d+\s*) # digits surrounded by spaces \) # literal right paren (?:\s*\+\s*)? # optionally followed by a concating '+' ) ''' , conv, tgt, flags=re.VERBOSE | re.IGNORECASE) </code></pre> <p>Prints:</p> <pre><code>|(SELECT TOP 1 CAST(name AS VARCHAR(8000)) FROM (SELECT TOP 1 colid, name FROM [Projects]..[syscolumns] WHERE xtype=U AND id = OBJECT_ID(EN_Empl) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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