<?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[switching letters with one regex]]></title><description><![CDATA[<p dir="auto">I have a few lines of text like so:</p>
<p dir="auto">nnseww<br />
wwwnensen</p>
<p dir="auto">Basically north, south, east and west. It’s not bad at all in PERL.</p>
<p dir="auto">$x =~ tr/nsew/snwe/;</p>
<p dir="auto">And I have a way to flip things around, but it’s messy.</p>
<ul>
<li>change n to z1</li>
<li>change s to z2</li>
<li>change e to z3</li>
<li>change w to z4</li>
<li>change z1 to s</li>
<li>change z2 to n</li>
<li>change z3 to w</li>
<li>change z4 to e</li>
</ul>
<p dir="auto">Now, this works, but it doesn’t feel as clean as, say, the perl script that translates…</p>
<p dir="auto">I also noticed <a href="http://superuser.com/questions/437652/notepad-how-to-replace-different-characters-with-corresponding-letters-at-onc" rel="nofollow ugc">this SuperUser thread</a>. Has there been any new regex stuff in the last 4 years, or will I just need to use PERL for this sort of thing?</p>
<p dir="auto">This is hardly a painful issue but I was just curious about if there was a quicker way to do things than what I found. Thanks!</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/12887/switching-letters-with-one-regex</link><generator>RSS for Node</generator><lastBuildDate>Fri, 15 May 2026 19:37:44 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/12887.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 09 Dec 2016 16:16:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to switching letters with one regex on Fri, 24 Feb 2017 20:35:07 GMT]]></title><description><![CDATA[<p dir="auto">Thanks, @Scott Sumner, I’ve been workin’. But now my work seems to lie in using RegExes to do things I couldn’t do easily without them, so here I am, back again.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/22128</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/22128</guid><dc:creator><![CDATA[glennfromiowa]]></dc:creator><pubDate>Fri, 24 Feb 2017 20:35:07 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 24 Feb 2017 19:20:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/glennfromiowa" aria-label="Profile: glennfromiowa">@<bdi>glennfromiowa</bdi></a></p>
<p dir="auto">You can find that in the Boost Regex Replace documentation, in the “Conditionals” section.  Here’s a possible link to that Boost documentation:  <a href="http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html" rel="nofollow ugc">http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html</a></p>
<p dir="auto">The example from <a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> 's usage is a special case of this explanation from those docs:</p>
<p dir="auto"><code>For example, the format string "(?1foo:bar)" will replace each match found with "foo" if the sub-expression $1 was matched, and with "bar" otherwise.</code></p>
<p dir="auto">BTW, <a class="plugin-mentions-user plugin-mentions-a" href="/user/glennfromiowa" aria-label="Profile: glennfromiowa">@<bdi>glennfromiowa</bdi></a> , Welcome Back to the Community after a long time away…where ya been?  :-D</p>
]]></description><link>https://community.notepad-plus-plus.org/post/22125</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/22125</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Fri, 24 Feb 2017 19:20:18 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 24 Feb 2017 03:51:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> said:</p>
<blockquote>
<ul>
<li>
<p dir="auto">Type, in the <strong>Find what:</strong> zone, <strong><code>(s)|(n)|(e)|(w)</code></strong></p>
</li>
<li>
<p dir="auto">Type, in the <strong>Replace with:</strong> zone, <strong><code>(?1n)(?2s)(?3w)(?4e)</code></strong></p>
</li>
</ul>
</blockquote>
<p dir="auto">I just stumbled across your post, and this is the <strong>first</strong> time I’ve ever seen this! Where is this documented? I mean, I think your example tells me <strong>everything</strong> I need to know, but I wish there were <strong>more examples</strong> like this in the Wiki page on <a href="http://docs.notepad-plus-plus.org/index.php/Regular_Expressions" rel="nofollow ugc">Regular Expressions</a>! It isn’t even mentioned that the <code>(?...)</code> construct can be used in the <em>Replace with</em> (<strong>Substitutions</strong>) part. I felt like it should be able to be done, and yet I hadn’t been able to figure out the syntax yet. So powerful, and yet way too much trial and error needed to discover what it can do!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/22110</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/22110</guid><dc:creator><![CDATA[glennfromiowa]]></dc:creator><pubDate>Fri, 24 Feb 2017 03:51:57 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Wed, 21 Dec 2016 23:46:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/claudia-frank" aria-label="Profile: Claudia-Frank">@<bdi>Claudia-Frank</bdi></a></p>
<p dir="auto">I was speaking of the Python re module; I forgot about the .research and .rereplace functions as I just tend to use the re ones.  It didn’t occur to me that the editor ones would call the same regex engine as an interactive find/replace, but it makes perfect sense that they would.  Thanks (again) for pointing out the obvious.</p>
<p dir="auto">I realize the regextester currently does only find, but was thinking that the next logical extension might be to do something with replace.  Thinking more about it, maybe that isn’t really practical…</p>
]]></description><link>https://community.notepad-plus-plus.org/post/20212</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/20212</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Wed, 21 Dec 2016 23:46:47 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Wed, 21 Dec 2016 23:34:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/scott-sumner" aria-label="Profile: Scott-Sumner">@<bdi>Scott-Sumner</bdi></a></p>
<pre><code>editor.rereplace(r'(s)|(n)|(e)|(w)', r'(?1n)(?2s)(?3w)(?4e)')
</code></pre>
<p dir="auto">did I miss something?<br />
Or did you use the python re module?<br />
In this case, yes, it doesn’t have the conditional substituion functionality.</p>
<p dir="auto">Just for completeness, RegexTester has been designed to test a regular expression,<br />
current version, and I assume this is still true even with the changes guy made,<br />
cannot do replaces.</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/20211</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/20211</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Wed, 21 Dec 2016 23:34:06 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Wed, 21 Dec 2016 21:23:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> said:</p>
<blockquote>
<p dir="auto">my own version of your excellent <a href="http://RegexTesterPro.py" rel="nofollow ugc">RegexTesterPro.py</a> script</p>
</blockquote>
<p dir="auto">Hello guy,</p>
<p dir="auto">I was just wondering if we are going to see your mentioned script HERE, perhaps sometime soon?  :)</p>
<p dir="auto">One thing that concerns me is that, while I’m sure the script is super-useful (as <a class="plugin-mentions-user plugin-mentions-a" href="/user/claudia-frank" aria-label="Profile: Claudia-Frank">@<bdi>Claudia-Frank</bdi></a> 's versions in the past have also been), it will do Python-flavored regex’s and not Notepad++ -flavored regexes.  For example, I have tried the “news” substitution that this thread began with, and while it works in N++, I haven’t been able to get something like it to work in Python–maybe I’m doing something wrong…</p>
]]></description><link>https://community.notepad-plus-plus.org/post/20207</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/20207</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Wed, 21 Dec 2016 21:23:12 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Sat, 17 Dec 2016 21:29:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/claudia-frank" aria-label="Profile: Claudia-Frank">@<bdi>Claudia-Frank</bdi></a></p>
<p dir="auto">HAHA…well, it’s pretty much an obvious rip-off of your earlier one, which hurt my brain when I first saw it.  :-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/20101</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/20101</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Sat, 17 Dec 2016 21:29:25 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Sat, 17 Dec 2016 21:26:40 GMT]]></title><description><![CDATA[<p dir="auto">Scott,<br />
a nice one - like it.<br />
Copied and backed up.</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/20100</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/20100</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Sat, 17 Dec 2016 21:26:40 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Sat, 17 Dec 2016 21:19:29 GMT]]></title><description><![CDATA[<p dir="auto">So, I, too, have a Perl background and have more than once, when doing Python, wished for something like Perl’s <strong>tr</strong> feature.  So this thread inspired me to put together something a little more generic than <a class="plugin-mentions-user plugin-mentions-a" href="/user/claudia-frank" aria-label="Profile: Claudia-Frank">@<bdi>Claudia-Frank</bdi></a> 's earlier script.  The following can be used in either Python or Notepad++'s Pythonscript:</p>
<pre><code>def translate(input_str, orig_chars, new_chars):
    assert len(orig_chars) &lt;= len(new_chars)
    trans_dict = {}
    for (j, v) in enumerate(orig_chars): trans_dict[v] = new_chars[j]
    return ''.join([ trans_dict.get(x, x) for x in input_str ])
</code></pre>
<p dir="auto">An example calling, using this thread’s original problem text, would be:</p>
<pre><code>x = 'nnswwennswwwwweee'
print x
y = translate(x, 'news', 'swen')
print y
</code></pre>
<p dir="auto">which will print:</p>
<pre><code>nnswwennswwwwweee
ssneewssneeeeewww
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/20098</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/20098</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Sat, 17 Dec 2016 21:19:29 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Tue, 13 Dec 2016 13:18:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a> Wow! Very well done and creative! Thanks also for the other links.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/claudia-frank" aria-label="Profile: Claudia-Frank">@<bdi>Claudia-Frank</bdi></a> I’m learning python, and a script like that helps show me its power. This looks like it can be extended nicely to other cases where I need to flip 2 strings or rotate 3. So I really appreciate that.</p>
<p dir="auto">I wish I’d remembered to check earlier…I need to go figure how to send alerts to my email. I thought it would’ve happened automatically. It’s pretty awesome, though, to learn about programming on a word processing forum. NotePad++ really has been a boon to me.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/19929</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/19929</guid><dc:creator><![CDATA[Andrew Schultz]]></dc:creator><pubDate>Tue, 13 Dec 2016 13:18:29 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 09 Dec 2016 21:24:40 GMT]]></title><description><![CDATA[<p dir="auto"><strong>Claudia</strong> and <strong>Jim</strong>,</p>
<p dir="auto"><strong>Claudia</strong>, oh no ! <strong>Definitively</strong> not ! It’s just that, when I see some <strong>changes</strong> of text, which involve <strong>current</strong> file ONLY, and which need ONLY <strong>one</strong> S/R, <strong>regular</strong> expressions seem to be, most of the time, the <strong>shorter</strong> way to get the job done !</p>
<hr />
<p dir="auto">BTW, I will send you, soon, my <strong>own</strong> version of your excellent <strong><a href="http://RegexTesterPro.py" rel="nofollow ugc">RegexTesterPro.py</a></strong> script. Just to know your <strong>feedback</strong> about my <strong>modifications</strong> and, ( again ! ) some <strong>suggestions</strong> :-))</p>
<p dir="auto">Cheers,</p>
<p dir="auto">guy038</p>
<p dir="auto">P. S. :</p>
<p dir="auto">BTW, for an quick <strong>oversight</strong> about the differences between <strong>Formal</strong> languages, <strong>Programming</strong> languages and <strong>Regular</strong> expressions, refer to that <strong>interesting</strong> article ( especially, the answers of <strong>babou</strong> and <strong>tsleyson</strong> !! )</p>
<p dir="auto"><a href="http://cs.stackexchange.com/questions/30639/what-is-the-relationship-between-programming-languages-regular-expressions-and" rel="nofollow ugc">http://cs.stackexchange.com/questions/30639/what-is-the-relationship-between-programming-languages-regular-expressions-and</a></p>
<hr />
<p dir="auto">Specifically, about <strong>regular</strong> expressions, read this <strong>Wikipedia</strong> article :</p>
<p dir="auto"><a href="https://en.wikipedia.org/wiki/Regular_expression#" rel="nofollow ugc">https://en.wikipedia.org/wiki/Regular_expression#</a></p>
<p dir="auto">And, particularly, the sections :</p>
<p dir="auto"><a href="https://en.wikipedia.org/wiki/Regular_expression#Formal_language_theory" rel="nofollow ugc">https://en.wikipedia.org/wiki/Regular_expression#Formal_language_theory</a></p>
<p dir="auto"><a href="https://en.wikipedia.org/wiki/Regular_expression#Patterns_for_non-regular_languages" rel="nofollow ugc">https://en.wikipedia.org/wiki/Regular_expression#Patterns_for_non-regular_languages</a></p>
]]></description><link>https://community.notepad-plus-plus.org/post/19825</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/19825</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 09 Dec 2016 21:24:40 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 09 Dec 2016 19:37:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jim-dailey" aria-label="Profile: Jim-Dailey">@<bdi>Jim-Dailey</bdi></a></p>
<p dir="auto">a vicious circle :-D</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/19821</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/19821</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Fri, 09 Dec 2016 19:37:13 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 09 Dec 2016 19:32:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/claudia-frank" aria-label="Profile: Claudia-Frank">@<bdi>Claudia-Frank</bdi></a>  Until you realize that regex is just another programming language. :-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/19820</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/19820</guid><dc:creator><![CDATA[Jim Dailey]]></dc:creator><pubDate>Fri, 09 Dec 2016 19:32:25 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 09 Dec 2016 19:10:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/guy038" aria-label="Profile: guy038">@<bdi>guy038</bdi></a><br />
what should I say - brilliant :-)<br />
Maybe we should think about rejecting all programming languages and do regex only ;-)</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/19819</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/19819</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Fri, 09 Dec 2016 19:10:27 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 09 Dec 2016 19:05:15 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <strong>Andrew Schultz</strong> and <strong>Claudia</strong>,</p>
<p dir="auto">This time, <strong>Claudia</strong>, my following <strong>regex</strong> S/R seems <strong>shorter</strong> than your <strong>Python</strong> script ;-))</p>
<p dir="auto">So, <strong>Andrew</strong>, just :</p>
<ul>
<li>
<p dir="auto">Select the <strong>Regular expression</strong> mode in the <strong>Replace</strong> dialog</p>
</li>
<li>
<p dir="auto">Type, in the <strong>Find what:</strong> zone, <strong><code>(s)|(n)|(e)|(w)</code></strong></p>
</li>
<li>
<p dir="auto">Type, in the <strong>Replace with:</strong> zone, <strong><code>(?1n)(?2s)(?3w)(?4e)</code></strong></p>
</li>
<li>
<p dir="auto">Click on the <strong>Replace All</strong> button</p>
</li>
</ul>
<p dir="auto">Et voilà !</p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/19817</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/19817</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 09 Dec 2016 19:05:15 GMT</pubDate></item><item><title><![CDATA[Reply to switching letters with one regex on Fri, 09 Dec 2016 17:21:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/andrew-schultz" aria-label="Profile: Andrew-Schultz">@<bdi>Andrew-Schultz</bdi></a></p>
<p dir="auto">if by, any chance, a python script can be used, than this one line could be helpful.</p>
<pre><code>editor.replaceSel(''.join([{'n':'s','s':'n','e':'w','w':'e'}.get(x, x) for x in editor.getSelText()]))
</code></pre>
<p dir="auto">It gets a selected text and replaces each n,s,e,w with their flipped counterparts.</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/19816</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/19816</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Fri, 09 Dec 2016 17:21:08 GMT</pubDate></item></channel></rss>