<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Regex error: (SyntaxError: invalid syntax) on Python Script Plugin]]></title><description><![CDATA[<p dir="auto">hello. In my previous post, I update this Python script for making multiple search and replace in all files. <strong>And works just fine.</strong> The regex is very simple in that case.</p>
<pre><code># -*- coding: utf-8 -*-
from __future__ import print_function

from Npp import *
import os
import sys

#----------

class T22601(object):

    def __init__(self):
        Path="C:\\python-test"
        for root, dirs, files in os.walk(Path):
            nested_levels = root.split('/')
            if len(nested_levels) &gt; 0:
                del dirs[:]
            for filename in files:		
                if filename[-5:] == '.html':
                    notepad.open(root + "\\" + filename)
                    console.write(root + "\\" + filename + "\r\n")
                    notepad.runMenuCommand("Encodage", "Convertir en UTF-8")
                    regex_find_repl_dict_list = [
                        { 'find' : r'foo', 'replace' : r'bar' },
                        { 'find' : r'bar', 'replace' : r'gaga' },
                    ]
                    editor.beginUndoAction()
                    for d in regex_find_repl_dict_list: editor.rereplace(d['find'], d['replace'])
                    editor.endUndoAction()
                    notepad.save()
                    notepad.close()
#---------

if __name__ == '__main__': T22601()
</code></pre>
<p dir="auto">Now, I test a much complex regex:</p>
<pre><code>line 24
    { 'find' : r'&lt;link (.*?)( rel='canonical'/&gt;)', 'replace' : r'&lt;link rel="canonical" \1 /&gt;' },
</code></pre>
<pre><code>line 24
   { 'find' : r'&lt;link (.*?)( rel='canonical'/&gt;)', 'replace' : r'&lt;link rel="canonical" \1 /&gt;' },
</code></pre>
<p dir="auto">and I got this error: <strong>SyntaxError: invalid syntax</strong> So it is something about regex. But what ?</p>
<p dir="auto">see this print screen.</p>
<p dir="auto"><img src="/assets/uploads/files/1649844195380-6e8403b7-8f8b-46e4-8f20-acfe584d1089-image.png" alt="6e8403b7-8f8b-46e4-8f20-acfe584d1089-image.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://community.notepad-plus-plus.org/topic/22844/regex-error-syntaxerror-invalid-syntax-on-python-script-plugin</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 06:29:24 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/22844.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 13 Apr 2022 10:04:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Regex error: (SyntaxError: invalid syntax) on Python Script Plugin on Wed, 13 Apr 2022 13:05:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rodica-f" aria-label="Profile: rodica-F">@<bdi>rodica-F</bdi></a></p>
<p dir="auto">On complex regex, remember this:</p>
<p dir="auto">First, you must put <strong><code>\</code></strong> before any <strong><code>'</code></strong> in your regex. otherwise it will give conflict with the Python syntax that also have the operator <strong><code>'</code></strong></p>
<p dir="auto">You had to remove the <strong><code>r</code></strong> after <code>find :</code> part. So, should be <code>'foo'</code> instead of <code>r'foo'</code></p>
<p dir="auto">In your default example should be (without : <strong><code>r</code></strong> in front of <code>'foo'</code></p>
<p dir="auto"><code>{ 'find' : 'foo', 'replace' : r'bar' },</code></p>
<p dir="auto">Your code, with the new and complex regex, should be like this.</p>
<p dir="auto">I write a second regex to see much better:</p>
<pre><code># -*- coding: utf-8 -*-
from __future__ import print_function

from Npp import *
import os
import sys

#-------------------------------------------------------------------------------

class T22601(object):

    def __init__(self):
        Path="C:\\python-test"
        for root, dirs, files in os.walk(Path):
            nested_levels = root.split('/')
            if len(nested_levels) &gt; 0:
                del dirs[:]
            for filename in files:		
                if filename[-5:] == '.html':
                    notepad.open(root + "\\" + filename)
                    console.write(root + "\\" + filename + "\r\n")
                    notepad.runMenuCommand("Encodage", "Convertir en UTF-8")
                    regex_find_repl_dict_list = [
                        { 'find' : '&lt;link (.*?)( rel=\'canonical\'/&gt;)', 'replace' : r'&lt;link rel="canonical" \1 /&gt;' },
                        { 'find' : '(&lt;meta content=\')(.*)(\' property=\'og:description\'/&gt;)', 'replace' : r'&lt;meta name="description" content="\2" /&gt;' },
                    ]
                    editor.beginUndoAction()
                    for d in regex_find_repl_dict_list: editor.rereplace(d['find'], d['replace'])
                    editor.endUndoAction()
                    notepad.save()
                    notepad.close()
#-------------------------------------------------------------------------------

if __name__ == '__main__': T22601()
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/76005</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76005</guid><dc:creator><![CDATA[Robin Cruise]]></dc:creator><pubDate>Wed, 13 Apr 2022 13:05:02 GMT</pubDate></item><item><title><![CDATA[Reply to Regex error: (SyntaxError: invalid syntax) on Python Script Plugin on Wed, 13 Apr 2022 12:43:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/rodica-f" aria-label="Profile: rodica-f">@<bdi>rodica-f</bdi></a></p>
<p dir="auto"><strong>Don’t</strong> use single quotes inside single quotes, or double quotes inside double quotes</p>
<p dir="auto"><strong>Do</strong> use double quotes inside single quotes, or single quotes inside double quotes</p>
]]></description><link>https://community.notepad-plus-plus.org/post/76002</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/76002</guid><dc:creator><![CDATA[Neil Schipper]]></dc:creator><pubDate>Wed, 13 Apr 2022 12:43:54 GMT</pubDate></item><item><title><![CDATA[Reply to Regex error: (SyntaxError: invalid syntax) on Python Script Plugin on Wed, 13 Apr 2022 10:12:19 GMT]]></title><description><![CDATA[<p dir="auto">maybe I forgot to use flags?</p>
<p dir="auto">something like: <code>flags=re.MULTILINE</code></p>
<pre><code>{ 'find' : r'&lt;link (.*?)( rel='canonical'/&gt;)', 'replace' : r'&lt;link rel="canonical" \1 /&gt;' }, flags=re.MULTILINE
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/75999</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/75999</guid><dc:creator><![CDATA[rodica F]]></dc:creator><pubDate>Wed, 13 Apr 2022 10:12:19 GMT</pubDate></item></channel></rss>