<?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[Replace - Regular Expression - need help on some regex snippets]]></title><description><![CDATA[<p dir="auto">Hi!</p>
<p dir="auto">I have to do a lot Search &amp; Replace all the time, but many of my RegEx snippets don’t work in some cases or don’t work at all. Would be nice if someone could take a look at them and:</p>
<ul>
<li>correct and tell what I did wrong</li>
<li>explain the corrected code</li>
<li>tell, if they are fail-safe or if they break in certain situations<br />
<code> </code></li>
</ul>
<p dir="auto"><strong>1 .  multiple replace</strong> <em>(f)</em><br />
Example: <code>ä Ä ż Ż</code> → <code>ae Ae z Z</code><br />
Find: <code>(ä)|(Ä)|(ż)|(Ż)</code><br />
Replace:	<code>(?1ae)(?2&amp;Ae)(?3z)(?4Z)</code></p>
<p dir="auto"><strong>2 . replace text but keep numbering</strong> <em>(f)</em> <em>(z)</em><br />
Example:<br />
<code>jnfvdsertzuikl 0006.jpg</code> → <code>blub 6.jpg</code><br />
<code>mnbvcxdfrtgzhjuki 0007.jpeg</code> → <code>blub 7.jpeg</code><br />
<code>mnbdsderzui 0008.bmp</code> → <code>blub 8.bmp</code><br />
Find: <code>.*([0-9]+)(\.\w+)$</code><br />
Replace:	<code>blub $1\2</code></p>
<p dir="auto"><strong>3 . replace but increment value</strong> <em>(w)</em> <em>(z)</em><br />
Example:<br />
<code>...images/69thStreet010.jpg</code> → <code>...images/xyz001.jpg</code><br />
<code>...images/69thStreet011.jpg</code> → <code>...images/xyz002.jpg</code><br />
<code>...images/69thStreet012.jpg</code> → <code>...images/xyz003.jpg</code><br />
Find: <code>images/69thStreet([0-9]+).jpg</code><br />
Replace: <code>images/xyz$1.jpg</code></p>
<p dir="auto"><strong>4 . remove all lines except those containing URI</strong> <em>(n)</em><br />
This bastard is really tricky! Haven’t yet figured out how get it done. Maybe this is beyond the possibilities of RegEx.</p>
<p dir="auto">Example:<br />
[08 Jun 15 21:57] * Joe: Hi<br />
check this out: <a href="http://files.example.com/blah/blub/picture.jpg" rel="nofollow ugc">http://files.example.com/blah/blub/picture.jpg</a><br />
[08 Jun 15 21:59] * John: Nice<br />
what about tis one<br />
<a href="https://www2.files3.example.com/asdf/qwerty/video.mp4" rel="nofollow ugc">https://www2.files3.example.com/asdf/qwerty/video.mp4</a><br />
…<br />
Find: <code>^(?!.*http(s)?:\/\/(\w+\.)+\w+\/(\w+\/)*\w+\.\w+)$</code><br />
Replace: <code>_clear_</code></p>
<p dir="auto">I had even more snippets, but I haven’t saved them and can’t remember what they were about.</p>
<p dir="auto">______________________________<br />
<em>(f)</em> fails with some expressions<br />
<em>(n)</em> doesn’t work at all<br />
<em>(w)</em> has worked so far<br />
<em>(z)</em> leading zeros are stripped</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/6817/replace-regular-expression-need-help-on-some-regex-snippets</link><generator>RSS for Node</generator><lastBuildDate>Wed, 19 Aug 2026 12:53:35 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/6817.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 13 Aug 2015 14:13:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Replace - Regular Expression - need help on some regex snippets on Fri, 14 Aug 2015 02:33:11 GMT]]></title><description><![CDATA[<p dir="auto">Hi <strong>Pete</strong>,</p>
<p dir="auto">I’m <strong>NOT</strong> able to modify an <strong>already posted</strong> message :-(, I always got the <strong>weird</strong> message :</p>
<blockquote>
<p dir="auto"><strong>Error</strong></p>
<p dir="auto">You are only allowed to edit posts for<br />
5 second(s) after posting</p>
</blockquote>
<p dir="auto">Don’t understand what that means ? Anyway, the final <strong>dotted</strong> line, of my <strong>initial</strong> post, must be understood :</p>
<ul>
<li>So, the final part <strong><code>.*\R</code></strong> does stand for the <strong>entire</strong> line, followed by its <strong>End of Line</strong> characters, <em>which need to be <strong>deleted</strong></em></li>
</ul>
<p dir="auto">Cheers,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/7480</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/7480</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 14 Aug 2015 02:33:11 GMT</pubDate></item><item><title><![CDATA[Reply to Replace - Regular Expression - need help on some regex snippets on Fri, 14 Aug 2015 02:00:34 GMT]]></title><description><![CDATA[<p dir="auto">Hello <strong>Pete</strong>,</p>
<p dir="auto">With the <strong>PCRE</strong> regex engine, introduced first, in N++ <strong>v6.0</strong>, by <strong>Dave Brotherstone</strong>, we can perform very <strong>powerful</strong> and intelligent searches/replacements !</p>
<p dir="auto">Concerning your <strong>first</strong> S/R, relative to <strong>accentuated</strong> characters, the syntax of <strong>simultaneous</strong> replacements is quite correct. However I’m wondering if, <em>in the <strong>replacement</strong> part</em>, the right form should be <strong><code>(?1ae)(?2Ae)(?3z)(?4Z)</code></strong> ( instead of <strong><code>(?1ae)(?2&amp;Ae)(?3z)(?4Z)</code></strong> ! )</p>
<p dir="auto">I can’t see any possibility, for that S/R to not work right, as the letters to search for, are clearly <strong>independent</strong>, providing the <strong>Match case</strong> option of the Replace dialog is <strong>CHECKED</strong>. If not, all the replaced characters would be letter case, as, in your S/R, the alternative (ä) is placed <strong>before</strong> the (Ä) and idem for letter ż.</p>
<p dir="auto">So, could you give me an <strong>example</strong> where this first S/R fails ?</p>
<hr />
<p dir="auto">Let’s see your <strong>second</strong> S/R. It’s an interesting example, as it allows us to speak about <strong>lazy</strong> and <strong>greedy</strong> quantifiers. For instance, if we consider the subject string <strong><code>12345a123456789b123456789b123456789b12345</code></strong>, just see the difference between the two simple regexes <strong><code>a.*b</code></strong> and <strong><code>a.*?b</code></strong></p>
<ul>
<li>
<p dir="auto">In the first case, the regex engine matches the <strong>longest</strong> string between the <strong>first</strong> found <strong>a</strong> and a letter <strong>b</strong>, so the string <strong>a123456789b123456789b123456789b</strong></p>
</li>
<li>
<p dir="auto">In the second case, the regex engine matches the <strong>shortest</strong> string between the <strong>first</strong> found <strong>a</strong> and a letter <strong>b</strong>, so the string <strong>a123456789b</strong></p>
</li>
<li>
<p dir="auto">In the first case, the <strong>dot</strong> do match the letter <strong>b</strong>, too. So, it will stop only when it reaches the last letter <strong>b</strong>, of the <strong>current</strong> line. It’s the standard behaviour and one speaks of <strong>greedy</strong> quantifiers.</p>
</li>
<li>
<p dir="auto">In the second case, due to the <strong>exclamation</strong> mark, after the quantifier, it will stop when it reaches the <strong>first found</strong> letter <strong>b</strong>, and one speaks of <strong>lazy</strong> quantifiers. If your regex engine <strong>can’t</strong> support this syntax, you need to use the the special regex <strong><code>a[^b\r\n]*b</code></strong> to get a <strong>similar</strong> result !</p>
</li>
</ul>
<p dir="auto">So, if we consider the string <strong><code>jnfvdsertzuikl 0006.jpg</code></strong>, with your search regex <strong><code>.*([0-9]+)(\.\w+)$</code></strong> :</p>
<ul>
<li>
<p dir="auto">The <strong><code>.*</code></strong> represents the string <strong>jnfvdsertzuikl 000</strong></p>
</li>
<li>
<p dir="auto">The <strong><code>([0-9]+)</code></strong> is, only, the <strong>number 6</strong> ( the <strong>last</strong> digit, before the file <strong>extension</strong> )</p>
</li>
</ul>
<p dir="auto">Now, using the syntax <strong><code>.*?([0-9]+)</code></strong> will include the <strong>leading</strong> zeros, in the replacement, because :</p>
<ul>
<li>
<p dir="auto">The <strong><code>.*?</code></strong> stands for the string <strong>jnfvdsertzuikl</strong>, with a <strong>space</strong> at the end</p>
</li>
<li>
<p dir="auto">The <strong><code>([0-9]+)</code></strong> will contain, this time, <strong>all</strong> the digits ( the <strong>0006</strong> string, before the file <strong>extension</strong> )</p>
</li>
</ul>
<p dir="auto">Moreover, adding the extension part, in the regex, is <strong>useless</strong>, as it’s kept <strong>unchanged</strong> during the S/R. So, assuming that any line ends with a <strong>number</strong>, followed by a file <strong>extension</strong>, your <strong>2nd</strong> S/R can be shorten in :</p>
<p dir="auto">SEARCH = <strong><code>.*?([0-9]+)</code></strong> OR <strong><code>.*?(\d+)</code></strong> and REPLACE = <strong><code>blub $1</code></strong> OR <strong><code>blub \1</code></strong></p>
<p dir="auto">With your syntax, the two strings <strong>mnbdsderzui 0008.bmp</strong> and <strong>mnbdsderzui 0018.bmp</strong> would have given the same result <strong>blub 8.bmp</strong> !</p>
<p dir="auto">If you prefer that the file name <strong>abc 0002.jpg</strong> gives <strong>blub 2.jpg</strong> and <strong>xyz0057.bmp</strong> gives <strong>blub 57.bmp</strong>, we’ll choose the search regex, below :</p>
<p dir="auto">SEARCH = <strong><code>.*?([1-9][0-9]*)</code></strong> OR <strong><code>.*?([1-9]\d*)</code></strong> and REPLACE = <strong><code>blub $1</code></strong> OR <strong><code>blub \1</code></strong></p>
<p dir="auto">This regex matches any possible sequence of characters till the <strong>first found</strong> digit, different from <strong>0</strong>, followed by a possible sequence of <strong>digits</strong>. In that regex, the possible <strong>leading</strong> zeros are included in the <strong><code>.*?</code></strong> form, at the <strong>beginning</strong> of the regex.</p>
<hr />
<p dir="auto">Concerning your <strong>3rd</strong> S/R, I quite <strong>surprised</strong> when you say that this S/R could <strong>increment</strong> numbers !? I rather think that it’s just one of the limits of regexes ! For instance, to change <strong>abc 003.jpg</strong> into <strong>abc 010.jpg</strong> ( <strong>offset = +7</strong> ), <strong>def 027.bmp</strong> into <strong>def 034</strong> ( <strong>+7</strong> ), and so on…, seems only possible <em>with a <strong>python</strong> or <strong>Gawk</strong> script</em> !</p>
<p dir="auto">Of course, you could use the same structure, as in your <strong>1st</strong> S/R:  SEARCH <strong><code>(003)|....|(027)|....</code></strong> and REPLACEMENT <strong><code>....(?{3}010)....(?{27}034)....</code></strong>, but it would be very fastidious for a <strong>big</strong> range of values !</p>
<p dir="auto">BTW, note that, in the replacement part, I enclosed the <strong>group</strong> number between <strong>braces</strong>, to separate the <strong>group</strong> number from the <strong>digits</strong> to replace. Indeed, without braces, the conditional replacement <strong><code>(?27034)</code></strong> would be quite <strong>ambiguous</strong> Would it mean :</p>
<ul>
<li>
<p dir="auto">If group 2 matches, replace with string <strong>7034</strong></p>
</li>
<li>
<p dir="auto">If group 27 matches, replace with string <strong>034</strong></p>
</li>
<li>
<p dir="auto">If group 270 matches, replace with string  <strong>34</strong> …</p>
</li>
</ul>
<p dir="auto">Anyway, this <strong>3rd</strong> S/R <strong>doesn’t</strong> give the same replacement file names, that you gave, in your post !</p>
<p dir="auto">Instead of your sequence <strong>images/xyz001.jpg</strong>, <strong>images/xyz002.jpg</strong>, <strong>images/xyz003.jpg</strong>,…, it, simply, gives the sequence <strong>images/xyz010.jpg</strong>, <strong>images/xyz011.jpg</strong>, <strong>images/xyz012.jpg</strong> !</p>
<hr />
<p dir="auto">Finally, let’s study your <strong>fourth</strong> and final S/R ( The <em>bastard</em> one !! ) I preferred to <strong>split</strong> the problem in <strong>two</strong> parts :</p>
<ul>
<li>
<p dir="auto">Firstly, find a suitable regex to exactly match <strong>YOUR</strong> specific kind of <strong>URL</strong></p>
</li>
<li>
<p dir="auto">Secondly, find a regex to delete any line, which <strong>DOESN’T</strong> contain your kind of URL</p>
</li>
</ul>
<p dir="auto">From the <strong>two</strong> examples of URL, that you gave in your post, these are set up from <strong>4</strong> parts</p>
<ul>
<li>
<p dir="auto">The string <strong><code>http://</code></strong>  or <strong><code>https://</code></strong></p>
</li>
<li>
<p dir="auto">The name of a <strong>site</strong></p>
</li>
<li>
<p dir="auto">An absolute <strong>pathname</strong></p>
</li>
<li>
<p dir="auto">A <strong>picture</strong> file name</p>
</li>
</ul>
<p dir="auto">However, the <strong>three</strong> last parts contain only <strong>lowercase</strong> letters, digits, the <strong>slash</strong> or the <strong>dot</strong>. So, a first try would give the regex <strong><code>https?://[\w/.]+</code></strong>. But, as some links may contain <strong>dashes</strong>, <strong>sharp</strong> characters and the <strong>percent</strong> sign ( as <strong>%20</strong> for space ), I would rather use the regex <strong><code>https?://[\w/.#%-]+</code></strong></p>
<p dir="auto">Note that the <strong>strict</strong> regex, which would allow <strong>lowercase</strong> letters only, would be <strong><code>https?://[\l\d_/.#%-]+</code></strong></p>
<hr />
<p dir="auto">Now, to get the <strong>final</strong> behaviour, we just have to use, from the location <strong>start of line</strong> ( assertion <strong>^</strong> ) a <strong>negative</strong> look-ahead to detect the NO-match of this <strong>URL</strong>, further on the <strong>current</strong> line. In that case, we’ll take all the characters of the <strong>current</strong> line <strong><code>.*</code></strong>, as well as its <strong>End of Line</strong> character(s) to delete them, in the replacement part.</p>
<p dir="auto">Therefore, the correct regex should be : SEARCH = <strong><code>^(?!.*https?://[\w/.#%-]+).*\R</code></strong> and REPLACE = <strong><code>NOTHING</code></strong></p>
<ul>
<li>
<p dir="auto">The first part <strong><code>^(?!.*https?://[\w/.#%-]+)</code></strong> which is evaluated, at position <strong>1</strong> of <strong>each</strong> line, look for a possible URL, inside the <strong>current</strong> line. Be aware that matches, in look-arounds, are <strong>never</strong> part of the <strong>final</strong> regex to search.</p>
</li>
<li>
<p dir="auto">So, the final part <strong>`.*\R``</strong> does stand for the entire line, followed by its <strong>End of Line</strong> characters, _ xhich need to be <strong>deleted</strong>_</p>
</li>
</ul>
<p dir="auto">For memory, the <strong><code>\R</code></strong> syntax matches, among other things, the string <strong><code>\r\n</code></strong>, in the <strong>Windows</strong> files, the <strong>\n</strong> string, in the <strong>Unix</strong> files or <strong>\r</strong> in some old <strong>MAC</strong> files</p>
<hr />
<p dir="auto">Hoping that some parts of my <strong>long</strong> post will be useful to you,</p>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
<p dir="auto">P.S. :</p>
<p dir="auto">You’ll find <strong>good documentation</strong>, about the new <strong>Boost C++ Regex library, v1.55.0</strong> ( similar to the <strong>PERL Regular Common Expressions, v1.48.0</strong> ), used by <strong><code>Notepad++</code></strong>, <em>since its <strong><code>6.0</code></strong> version</em>, at the <strong>TWO</strong> addresses below :</p>
<p dir="auto"><a href="http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html" rel="nofollow ugc">http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html</a></p>
<p dir="auto"><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>
<ul>
<li>
<p dir="auto">The <strong>FIRST</strong> link explains the <strong>syntax</strong>, of regular expressions, in the <strong>SEARCH</strong> part</p>
</li>
<li>
<p dir="auto">The <strong>SECOND</strong> link explains the <strong>syntax</strong>, of regular expressions, in the <strong>REPLACEMENT</strong> part</p>
</li>
</ul>
]]></description><link>https://community.notepad-plus-plus.org/post/7479</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/7479</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Fri, 14 Aug 2015 02:00:34 GMT</pubDate></item></channel></rss>