<?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[(?(backreference)true-regex|false-regex)]]></title><description><![CDATA[<p dir="auto">123-456-7890           				//line1<br />
(123)456-7890					//line2<br />
(123)-456-7890					//line3<br />
(123-456-7890					//line4<br />
1234567890						//line5<br />
123 456 7890						//line6</p>
<p dir="auto">(()?\d{3}(?(1))|-)\d{3}-\d{4}<br />
I expected that it matchs  line1 or  lin2,   but line4</p>
<p dir="auto">Is there some error ?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/23418/backreference-true-regex-false-regex</link><generator>RSS for Node</generator><lastBuildDate>Wed, 17 Jun 2026 13:23:28 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/23418.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 28 Aug 2022 01:27:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Mon, 05 Sep 2022 22:12:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/wing-yang" aria-label="Profile: wing-yang">@<bdi>wing-yang</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/mapje71" aria-label="Profile: mapje71">@<bdi>mapje71</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">In the <strong>last</strong> part of my <strong>previous</strong> post, I proposed a regex which does <strong>not</strong> use any <strong>conditional</strong> statement :</p>
<p dir="auto"><strong><code>(?x) ^ [^(\r\n]* (?: \( (\d{3}) \) | (?1) - ) (?1) - \d{4}</code></strong></p>
<p dir="auto">However, this regex can simply be <strong>improved</strong> by using the <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a>’s <strong>look-behind</strong> structure, instead of the <strong>negative class</strong> character :</p>
<p dir="auto"><strong><code>(?x)  (?&lt;!\()  (?: \( (\d{3}) \) | (?1) - ) (?1) - \d{4}</code></strong></p>
<p dir="auto">which allows to get <strong>several</strong> occurences in a <strong>single</strong> line, as below :</p>
<pre><code class="language-diff">abc (123)456-7890 def 123-456-7890 ghi
</code></pre>
<p dir="auto">BR</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79559</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79559</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Mon, 05 Sep 2022 22:12:58 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Sat, 03 Sep 2022 10:07:42 GMT]]></title><description><![CDATA[<p dir="auto">Hello, <a class="plugin-mentions-user plugin-mentions-a" href="/user/wing-yang" aria-label="Profile: wing-yang">@<bdi>wing-yang</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a>, <a class="plugin-mentions-user plugin-mentions-a" href="/user/mapje71" aria-label="Profile: mapje71">@<bdi>mapje71</bdi></a> and <strong>All</strong>,</p>
<p dir="auto">Just back from holidays, in <strong>Brittany</strong> : one of the <em>few</em> <strong>temperate</strong> regions in the world !! We can <strong>fully</strong> rest, during the nights, with temperatures always under <strong>20°</strong> !</p>
<hr />
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wing-yang" aria-label="Profile: wing-yang">@<bdi>wing-yang</bdi></a>, when you said to <a class="plugin-mentions-user plugin-mentions-a" href="/user/mapje71" aria-label="Profile: mapje71">@<bdi>mapje71</bdi></a> :</p>
<blockquote>
<p dir="auto">I want to exercise backreference<br />
thank you</p>
</blockquote>
<p dir="auto">Your statement is not totally <strong>exact</strong> ! Indeed, you want to exercise the <strong>conditional</strong> regex feature, whose definition is :</p>
<p dir="auto"><strong><code>(?(...) THEN part [ | ELSE part ] )</code></strong></p>
<p dir="auto">where the <strong>condition</strong> <strong><code>(...)</code></strong> can be, either :</p>
<ul>
<li>
<p dir="auto"><strong><code>(N)</code></strong> :  A group <strong>number</strong>, <em>previously</em> created</p>
</li>
<li>
<p dir="auto"><strong><code>(&lt;Name&gt;)</code></strong> or <strong><code>('Name')</code></strong> : A <strong>named</strong> group, <em>previously</em> created</p>
</li>
<li>
<p dir="auto"><strong><code>(?=...)</code></strong> or <strong><code>(?!...)</code></strong> : A <em>positive</em> or <em>negative</em> <strong>look-ahead</strong></p>
</li>
<li>
<p dir="auto"><strong><code>(?&lt;=...)</code></strong> or <strong><code>(?&lt;!...)</code></strong> : A <em>positive</em> or <em>negative</em> <strong>look-behind</strong></p>
</li>
<li>
<p dir="auto"><strong><code>(R)</code></strong> : A <strong>recursive</strong> reference to the <strong>whole</strong> regex</p>
</li>
<li>
<p dir="auto"><strong><code>(Rn)</code></strong> : A <strong>recursive</strong> reference to  the <strong>unnamed</strong> group number <strong>n</strong></p>
</li>
<li>
<p dir="auto"><strong><code>(R&amp;Name)</code></strong> : A <strong>recursive</strong> reference to the <strong>named</strong> group <strong>name</strong></p>
</li>
</ul>
<p dir="auto">And, of course, the <strong><code>?(1)</code></strong> part of your regex refers to the <strong>optional</strong> syntax <strong><code>(\()?</code></strong>, created before, which describe <strong>two</strong> cases :</p>
<ul>
<li>
<p dir="auto"><strong><code>(\(){1}</code></strong> : <strong>defined</strong> group <strong><code>1</code></strong>, containing an <strong>opening</strong> parenthese =&gt; The part <strong>THEN</strong> is used</p>
</li>
<li>
<p dir="auto"><strong><code>(\(){0}</code></strong> : <strong>non-defined</strong> group <strong><code>1</code></strong>, as group <strong><code>1</code></strong> contains <strong>nothing</strong> =&gt; The part <strong>ELSE</strong> is used</p>
</li>
</ul>
<hr />
<p dir="auto">Now, the <strong>last</strong> formulation of <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a>, expressed with the <strong>free-spacing</strong> mode :</p>
<p dir="auto"><strong><code>(?x)  (?&lt;!\()  (\()?  \d{3}  (?(1) \) | -)  \d{3}-\d{4}</code></strong></p>
<p dir="auto">works nicely against this <strong>test</strong> list :</p>
<pre><code class="language-diff">123-456-7890               //  Line_01   OK
(123)456-7890              //  Line_02   OK
(123)-456-7890             //  Line_03
(123-456-7890              //  Line_04
1234567890                 //  Line_05
123 456 7890               //  Line_06
blankblank(123-456-7890    //  Line_07
blankblank123-456-7890     //  Line_08   OK
blankblank((123)-456-7890  //  Line_09
blankblank(123)456-7890    //  Line_10   OK
blankblank)123-456-7890    //  Line_11   OK
  (123)456-7890            //  Line_12   OK
  123-456-7890             //  Line_13   OK
blankblank(123))-456-7890  //  Line_14
blankblank((123))-456-7890 //  Line_15
blankblank((123))-456-7890 //  Line_16
  (123))-456-7890          //  Line_17
  ((123))-456-7890         //  Line_18
  ((123))-456-7890         //  Line_19
</code></pre>
<hr />
<p dir="auto">Note that if you don’t want to use a <strong>conditional</strong> statemment, we can improve the <a class="plugin-mentions-user plugin-mentions-a" href="/user/mapje71" aria-label="Profile: mapje71">@<bdi>mapje71</bdi></a> solution, and get the <strong>equivalent</strong> regex :</p>
<p dir="auto"><strong><code>(?x) ^ [^(\r\n]* (?: \( (\d{3}) \) | (?1) - ) (?1) - \d{4}</code></strong></p>
<p dir="auto">where :</p>
<ul>
<li>
<p dir="auto"><strong><code>^ [^(\r\n]*</code></strong> is a <strong>leading</strong> range of characters, possibly <strong>null</strong>, <strong>different</strong> from an <strong>opening</strong> parenthese and a <strong>line-break</strong> char</p>
</li>
<li>
<p dir="auto"><strong><code>(?: \( (\d{3}) \) | (?1) - )</code></strong> represents a <strong>non-capturing</strong> group, containing the <strong>two</strong> alternatives</p>
</li>
<li>
<p dir="auto">The <strong><code>(?1)</code></strong> syntax represents a <strong>subroutine call</strong> to the regex part <strong><code>\d[3}</code></strong></p>
</li>
</ul>
<p dir="auto">Best Regards,</p>
<p dir="auto">guy038</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79526</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79526</guid><dc:creator><![CDATA[guy038]]></dc:creator><pubDate>Sat, 03 Sep 2022 10:07:42 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Thu, 01 Sep 2022 01:59:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a><br />
I see.  I  apprecate  your help.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79481</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79481</guid><dc:creator><![CDATA[wing yang]]></dc:creator><pubDate>Thu, 01 Sep 2022 01:59:13 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Tue, 30 Aug 2022 14:17:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wing-yang" aria-label="Profile: wing-yang">@<bdi>wing-yang</bdi></a> ,</p>
<p dir="auto">Have you not noticed that when you type a regular expression here, not all the characters show up?  Do you not realize that this makes it harder to answer your questions?  Do you really want us to have to do extra work just to understand your question, let alone answer it?  Have you not read the “Please Read Before Posting” and other FAQ entries that tell you how to format forum posts, especially <a href="https://community.notepad-plus-plus.org/topic/22022/faq-desk-template-for-search-replace-questions">FAQ: Template for Search&amp;Replace Questions</a>, which explains in excruciating detail how to ask successful search-and-replace questions?</p>
<p dir="auto">When you are supplying data or regex, you can select the text and hit the <code>&lt;/&gt;</code> button, which will put a line of <code>```</code> before and after your selected text.  It will look like this while you are editing:</p>
<pre><code class="language-txt">```
123-456-7890 //line1
(123)456-7890 //line2
(123)-456-7890 //line3
(123-456-7890 //line4
1234567890 //line5
123 456 7890 //line6
blankblank(123-456-7890 //line7
blankblank123-456-7890 //line8

^(\()?\d{3}(?(1)\)|-)\d{3}-\d{4} miss line8
```
</code></pre>
<p dir="auto">And then it will render like this:</p>
<pre><code>123-456-7890 //line1
(123)456-7890 //line2
(123)-456-7890 //line3
(123-456-7890 //line4
1234567890 //line5
123 456 7890 //line6
blankblank(123-456-7890 //line7
blankblank123-456-7890 //line8

^(\()?\d{3}(?(1)\)|-)\d{3}-\d{4} miss line8
</code></pre>
<p dir="auto">Notice that now, we can read your whole regular expression that you tried, without the characters being missing.</p>
<p dir="auto">Now, back to your problem statement:</p>
<blockquote>
<pre><code>^(\()?\d{3}(?(1)\)|-)\d{3}-\d{4} miss line8
</code></pre>
</blockquote>
<p dir="auto">Of course it misses line 8, because you changed the definition of what you want it to match.  We’ve given you enough to answer your original question.  At some point, you have to try to understand what you’ve been given, and try to modify it for your own needs.  But I will give you one last freebie.</p>
<p dir="auto">All your example data had your text that you wanted to match starting at the beginning of the line, so we included a <code>^</code> anchor to make it start at the beginning of the line, because that was one way to prevent line 4 from matching.  Now line 8 does <em>not</em> start the phone number at the beginning of the line.  Take out that character, so that you have the regex <code>(\()?\d{3}(?(1)\)|-)\d{3}-\d{4}</code>, and it will match line 8.</p>
<p dir="auto">At that point, you are going to complain that “<code>(\()?\d{3}(?(1)\)|-)\d{3}-\d{4}</code> is what I originally had: it also matches line 4 and line 7 (and I don’t want that)”.  And this is because it tried to match <code>(###)</code> and couldn’t, so then it went to the alternation which wants to match <code>###-</code> which it could do.</p>
<p dir="auto">To fix that, since we can no longer anchor to the beginning of the line, we need to instead tell it that <code>(</code> cannot come before, using a negative lookbehind: <code>(?&lt;!\()</code> says that “the character <em>before</em> the match cannot be a literal <code>(</code>”.</p>
<p dir="auto">The final regular expression I am going to give you is thus</p>
<ul>
<li><code>(?&lt;!\()(\()?\d{3}(?(1)\)|-)\d{3}-\d{4}</code></li>
</ul>
<p dir="auto">Per the screenshot, it matches lines 1, 2, and 8 without matching the other lines.</p>
<p dir="auto"><img src="/assets/uploads/files/1661866536592-ef86e154-5363-4324-a121-929f2787c6e1-image.png" alt="ef86e154-5363-4324-a121-929f2787c6e1-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">From here, you can use the references below to start learning more about the individual components of the regex yourself.  And please note that we are not a generic “help me learn regex” site: we focus on Notepad++, and while regexes are a part of Notepad++, they are only a part, and we will stop answering regex questions if it gets in the way of actually talking about Notepad++ itself.</p>
<p dir="auto">-—</p>
<h3>Useful References</h3>
<ul>
<li><a href="https://community.notepad-plus-plus.org/topic/21965/please-read-before-posting">Please Read Before Posting</a></li>
<li><a href="https://community.notepad-plus-plus.org/topic/22022/template-for-search-replace-questions">Template for Search/Replace Questions</a></li>
<li><a href="https://community.notepad-plus-plus.org/topic/15765/faq-desk-where-to-find-regular-expressions-regex-documentation">FAQ: Where to find regular expressions (regex) documentation</a></li>
<li><a href="https://npp-user-manual.org/docs/searching/#regular-expressions" rel="nofollow ugc">Notepad++ Online User Manual: Searching/Regex</a></li>
</ul>
<p dir="auto">-—</p>
<p dir="auto"><em><strong>Please note</strong>: This Community Forum is not a data transformation service; you should not expect to be able to always say “I have data like X and want it to look like Y” and have us do all the work for you.  If you are new to the Forum, and new to regular expressions, we will often give help on the first one or two data-transformation questions, especially if they are well-asked and you show a willingness to learn; and we will point you to the documentation where you can learn how to do the data transformations for yourself in the future.  But if you repeatedly ask us to do your work for you, you will find that the patience of usually-helpful Community members wears thin.  The best way to learn regular expressions is by experimenting with them yourself, and getting a feel for how they work; having us spoon-feed you the answers without you putting in the effort doesn’t help you in the long term and is uninteresting and annoying for us.</em></p>
]]></description><link>https://community.notepad-plus-plus.org/post/79433</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79433</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 30 Aug 2022 14:17:01 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Tue, 30 Aug 2022 11:30:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a><br />
123-456-7890 //line1<br />
(123)456-7890 //line2<br />
(123)-456-7890 //line3<br />
(123-456-7890 //line4<br />
1234567890 //line5<br />
123 456 7890 //line6<br />
blankblank(123-456-7890 //line7<br />
blankblank123-456-7890 //line8</p>
<p dir="auto">^(()?\d{3}(?(1))|-)\d{3}-\d{4} miss line8</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79428</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79428</guid><dc:creator><![CDATA[wing yang]]></dc:creator><pubDate>Tue, 30 Aug 2022 11:30:27 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Sun, 28 Aug 2022 16:45:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wing-yang" aria-label="Profile: wing-yang">@<bdi>wing-yang</bdi></a> ,</p>
<blockquote>
<p dir="auto">I want to exercise backreference</p>
</blockquote>
<p dir="auto">Your original expression was doing the backreference correctly.  The problem is the expression was not anchored, so line four was matching the non-parenthesis side of the conditional starting on the <code>1</code> after failing to match the paren version starting on the <code>(</code>.  If you anchor to the beginning of the line, <code>^(\()?\d{3}(?(1)\)|-)\d{3}-\d{4}</code>, then the expression just matches lines 1 and 2.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79393</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79393</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Sun, 28 Aug 2022 16:45:43 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Sun, 28 Aug 2022 15:11:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mapje71" aria-label="Profile: MAPJe71">@<bdi>MAPJe71</bdi></a><br />
I want to exercise backreference<br />
thank you</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79390</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79390</guid><dc:creator><![CDATA[wing yang]]></dc:creator><pubDate>Sun, 28 Aug 2022 15:11:17 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Sun, 28 Aug 2022 15:06:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a><br />
yes .  but not line4.<br />
thank you</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79389</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79389</guid><dc:creator><![CDATA[wing yang]]></dc:creator><pubDate>Sun, 28 Aug 2022 15:06:25 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Sun, 28 Aug 2022 14:46:13 GMT]]></title><description><![CDATA[<p dir="auto">Why not use <code>(\(\d{3}\)|\d{3}-)\d{3}-\d{4}</code></p>
]]></description><link>https://community.notepad-plus-plus.org/post/79385</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79385</guid><dc:creator><![CDATA[MAPJe71]]></dc:creator><pubDate>Sun, 28 Aug 2022 14:46:13 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Sun, 28 Aug 2022 13:00:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wing-yang" aria-label="Profile: wing-yang">@<bdi>wing-yang</bdi></a> said in <a href="/post/79375">(?(backreference)true-regex|false-regex)</a>:</p>
<blockquote>
<p dir="auto">I expected that it matchs  line1 or  lin2,   but line4</p>
</blockquote>
<p dir="auto">I think you mean: “I expected that it matches line1 or  line2,   but not line4” ?</p>
<p dir="auto">I guess you’d have to say what it is about line4 that makes you not want to match it?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/79382</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79382</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sun, 28 Aug 2022 13:00:30 GMT</pubDate></item><item><title><![CDATA[Reply to (?(backreference)true-regex|false-regex) on Sun, 28 Aug 2022 02:12:17 GMT]]></title><description><![CDATA[<p dir="auto">Here is the same example text and regex, inside a code block, so that people can see the actual data and regex</p>
<pre><code>123-456-7890           				//line1
(123)456-7890					//line2
(123)-456-7890					//line3
(123-456-7890					//line4
1234567890						//line5
123 456 7890						//line6


(\()?\d{3}(?(1)\)|-)\d{3}-\d{4}         
</code></pre>
<p dir="auto">-—</p>
<h3>Useful References</h3>
<ul>
<li><a href="https://community.notepad-plus-plus.org/topic/21965/please-read-before-posting">Please Read Before Posting</a></li>
<li><a href="https://community.notepad-plus-plus.org/topic/22022/template-for-search-replace-questions">Template for Search/Replace Questions</a></li>
<li><a href="https://community.notepad-plus-plus.org/topic/15765/faq-desk-where-to-find-regular-expressions-regex-documentation">FAQ: Where to find regular expressions (regex) documentation</a></li>
<li><a href="https://npp-user-manual.org/docs/searching/#regular-expressions" rel="nofollow ugc">Notepad++ Online User Manual: Searching/Regex</a></li>
</ul>
]]></description><link>https://community.notepad-plus-plus.org/post/79376</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/79376</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Sun, 28 Aug 2022 02:12:17 GMT</pubDate></item></channel></rss>