• Login
Community
  • Login

Find-in-FIles: Can’t Replace Multiple Instances of Word

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
regex html
48 Posts 5 Posters 7.1k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S
    Sylvester Bullitt @PeterJones
    last edited by Sylvester Bullitt Feb 5, 2024, 8:22 PM Feb 5, 2024, 8:13 PM

    @PeterJones I tried the regex modification you suggested

    (?:lyrics-text|\G).+?(?<!^)(?<!<p>)(?<!<p class="chorus">)\Kstar(?=(.+?</div>))
    

    To keep it simple, I ran it against a single file in the editor (rather than multiple files on disk). The file text that was in the editor:

    <!DOCTYPE HTML>
    <html lang="en-us">
    
    <head>
    <meta charset="utf-8">
    <title>Twinkle, Twinkle, Little Star</title>
    <meta name="description" content="Words: Jane Taylor, 1806. Music: ___, ___">
    <meta name="keywords" content="Jane Taylor">
    <link rel="stylesheet" href="../../css/hymn.css">
    <script src="../../js/jquery.js"></script>
    <script src="../../js/base.js"></script>
    <script src="../../js/hymn.js"></script>
    <link rel="prev" href="../../htm/h/e/w/o/hewonsav.htm">
    <link rel="next" href="../../htm/h/e/s/a/hesallwo.htm">
    <link rel="up" href="../../ttl/ttl-h.htm">
    </head>
    
    <body>
    
    <section id="preface">
    <h1 class="screen-reader-only">Introduction</h1>
    <div class="preface-text">
    <p><span class="lead">Words:</span> <a href="../../bio/t/a/y/l/taylor_jane.htm">Jane Tay­lor</a>, 1806.</p>
    <p><span class="lead">Music:</span> John Doe  (<a href="../../mid/d/u/m/m/dummy.mid" title="Listen to music, MIDI format">🔊</a> <a href="../../pdf/en/d/u/m/m/Dummy.pdf" title="Download score, PDF format">pdf</a> <a href="../../nwc/d/u/m/m/Dummy.nwc" title="Download score, Noteworthy Composer format">nwc</a>).</p>
    </div>
    </section>
    
    <p>This page is used to test glo­bal search-and-replace us­ing re­gu­lar ex­pressions.</p>
    
    <section class="lyrics">
    <h1 class="screen-reader-only">Lyrics</h1>
    <div class="stanzas"><div class="lyrics-text mc ll">
    <p>Twinkle, twinkle, little star,<br>
    How I wonder what you are!<br>
    Up above the world so high,<br>
    Like a diamond in the sky.</p>
    <p>When the blazing sun is gone,<br>
    When he nothing shines upon,<br>
    Then you show your little light,<br>
    Twinkle, twinkle, all the night.</p>
    <p>Then the trav’ller in the dark,<br>
    Thanks you for your tiny spark,<br>
    He could not see which way to go,<br>
    If you did not twinkle so.</p>
    <p>In the dark blue sky you keep,<br>
    And often thro’ my curtains peep,<br>
    For you never shut your eye,<br>
    Till the sun is in the sky.</p>
    <p>’Tis your bright and tiny spark,<br>
    Lights the trav’ller in the dark:<br>
    Tho’ I know not what you are,<br>
    Twinkle, twinkle, little star.</p>
    </div></div>
    </section>
    
    </body>
    </html>
    

    I put the cursor at the beginning of the file, clicked the Find Next button. and got this error .

    If I click the Replace All button instead, a different message appears: Replace All: 0 occurrences were replaced in entire file.

    P 2 Replies Last reply Feb 5, 2024, 8:26 PM Reply Quote 0
    • P
      PeterJones @Sylvester Bullitt
      last edited by Feb 5, 2024, 8:26 PM

      @Sylvester-Bullitt said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

      The file text that was in the editor:

      I tried that text with your original regex from your first post, and got the same result.

      As I said, “if your original regex was working for doing a single replacement that had to be run multiple times”. Your text didn’t match your regex even once, thus the “if” condition was not met, and you should not expect my modification to work.

      S P 2 Replies Last reply Feb 5, 2024, 8:35 PM Reply Quote 0
      • S
        Sylvester Bullitt @PeterJones
        last edited by Feb 5, 2024, 8:35 PM

        @PeterJones Oops. Mea culpa.

        Forgot to change the search mode to Regular Expression. After doing that, and clicking Replace All, all 3 occurrences were replaced in one fell swoop. Sorry for the confusion.

        My next step is to try it on multiple files on the hard disk.

        Whew!

        C 1 Reply Last reply Feb 5, 2024, 8:53 PM Reply Quote 1
        • P
          PeterJones @PeterJones
          last edited by Feb 5, 2024, 8:37 PM

          @PeterJones said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

          I tried that text with your original regex from your first post, and got the same result.

          My test of your regex+data was failing for a different reason than your test had failed: as you said, yours failed because you forgot to enable Regular Expression mode.

          My test, on the other hand, failed because I didn’t have “. matches newline” checkmarked. Once I did that, I could get the search to work with either your original or my edited regex.

          S 1 Reply Last reply Feb 5, 2024, 8:39 PM Reply Quote 0
          • S
            Sylvester Bullitt @PeterJones
            last edited by Feb 5, 2024, 8:39 PM

            @PeterJones I love it when a plan comes together!

            Will let you know how testing goes “in the wild” (i.e., on actual files).

            S 1 Reply Last reply Feb 5, 2024, 9:08 PM Reply Quote 0
            • P
              PeterJones @Sylvester Bullitt
              last edited by Feb 5, 2024, 8:49 PM

              @Sylvester-Bullitt said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

              (?:lyrics-text|\G).+?(?<!^)(?<!<p>)(?<!<p class="chorus">)\Kstar(?=(.+?</div>))
              …
              all 3 occurrences were replaced in one fell swoop.

              I was surprised it was 3 occurrences, because the first occurrence was before lyrics-text.

              I was reminded that the \G can actually match the start of the text under certain circumstances. This isn’t 100% clear in the User Manual, but in the Boost Regex documentation that it links to, it says (emphasis mine) ,

              The sequence \G matches only at the end of the last match found, or at the start of the text being matched if no previous match was found.

              To prevent \G from matching the start, you need to make sure the first alternative consumes the \A: FIND = (?s)(\A.*?lyrics-text|\G).+?(?<!^)(?<!<p>)(?<!<p class="chorus">)\Kstar(?=(.+?</div>))
              With that, it only finds and replaces 2 in your “Twinkle Twinkle” file, instead of 3.

              P S C 3 Replies Last reply Feb 5, 2024, 8:58 PM Reply Quote 1
              • C
                Coises @Sylvester Bullitt
                last edited by Coises Feb 5, 2024, 8:54 PM Feb 5, 2024, 8:53 PM

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • P
                  PeterJones @PeterJones
                  last edited by Feb 5, 2024, 8:58 PM

                  @PeterJones said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                  This isn’t 100% clear in the User Manual,

                  I have tweaked the UM to include the phrase from the boost manual, to make it more clear. It is doubtful anything I write, especially regarding regular expressions, can be 100% clear. ;-)

                  1 Reply Last reply Reply Quote 0
                  • S
                    Sylvester Bullitt @PeterJones
                    last edited by Feb 5, 2024, 9:02 PM

                    @PeterJones Peter, I’m unfamiliar with the syntax (?:lyrics-text|\G). It resembles a lookbehind, but all the lookbehinds I’ve seen look like (? <=a) (i.e., no colon). What exactly is this thing? Where is it documented?

                    P 1 Reply Last reply Feb 5, 2024, 9:19 PM Reply Quote 0
                    • C
                      Coises @PeterJones
                      last edited by Feb 5, 2024, 9:07 PM

                      @PeterJones said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                      To prevent \G from matching the start, you need to make sure the first alternative consumes the \A: FIND = (?s)(\A.*?lyrics-text|\G).+?(?<!^)(?<!<p>)(?<!<p class=“chorus”>)\Kstar(?=(.+?</div>))
                      With that, it only finds and replaces 2 in your “Twinkle Twinkle” file, instead of 3.

                      It might not matter in @Sylvester-Bullitt’s application, but it should be noted that if the file does not contain the text lyrics-text at all, the expression given will replace every occurrence of star.

                      S 1 Reply Last reply Feb 5, 2024, 9:11 PM Reply Quote 1
                      • S
                        Sylvester Bullitt @Sylvester Bullitt
                        last edited by Feb 5, 2024, 9:08 PM

                        @Sylvester-Bullitt said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                        @PeterJones I love it when a plan comes together!

                        Will let you know how testing goes “in the wild” (i.e., on actual files).

                        Here’s the problem file:

                        <!DOCTYPE HTML>
                        <html lang="en-us">
                        
                        <head>
                        <meta charset="utf-8">
                        <title>Mary Had a Little Lamb</title>
                        <meta name="description" content="Words: Sarah Hale, 1830. Music: None.">
                        <meta name="keywords" content="Sarah Hale">
                        <link rel="stylesheet" href="../../css/hymn.css">
                        <script src="../../js/jquery.js"></script>
                        <script src="../../js/base.js"></script>
                        <script src="../../js/hymn.js"></script>
                        <link rel="prev" href="../../htm/h/e/w/o/hewonsav.htm">
                        <link rel="next" href="../../htm/h/e/s/a/hesallwo.htm">
                        <link rel="up" href="../../ttl/ttl-h.htm">
                        </head>
                        
                        <body>
                        
                        <section id="preface">
                        <h1 class="screen-reader-only">Introduction</h1>
                        <div class="preface-text">
                        <p><span class="lead">Words:</span> <a href="../../bio/h/a/l/e/hale_sjb.htm">Sarah J. Hale</a>, 1830.</p>
                        <p><span class="lead">Music:</span> John Doe  (<a href="../../mid/d/u/m/m/dummy.mid" title="Listen to music, MIDI format">🔊</a> <a href="../../pdf/en/d/u/m/m/Dummy.pdf" title="Download score, PDF format">pdf</a> <a href="../../nwc/d/u/m/m/Dummy.nwc" title="Download score, Noteworthy Composer format">nwc</a>).</p>
                        </div>
                        </section>
                        
                        <p>This page is used to test glo­bal search-and-replace us­ing re­gu­lar ex­pressions. </p>
                        
                        <section class="lyrics">
                        <div class="stanzas"><div class="lyrics-text mc ll">
                        <p>Mary had a little lambkin,<br>
                        Its fleece was white as snow.<br>
                        And everywhere that Mary went,<br>
                        The lamb was sure to go.<br>
                        He followed her to school one day,<br>
                        That was against the rule.<br>
                        It made the children laugh and play<br>
                        To see a lamb at school.</p>
                        <p>And so the teacher turned him out,<br>
                        But still he lingered near,<br>
                        And waited patiently about<br>
                        Till Mary did appear.<br>
                        And then he ran to her, and laid<br>
                        His head upon her arm,<br>
                        As if he said <q>I’m not afraid,<br>
                        You’ll keep me from all harm.</q></p>
                        <p><q>What makes the lamb love Mary so?</q><br>
                        The eager children cry.<br>
                        <q>‘Oh, Mary loves the lamb, you know,</q><br>
                        The teacher did reply.<br>
                        <q>And you each gentle animal<br>
                        In confidence may bind,<br>
                        And make them follow at your call,<br>
                        If you are always kind.</q> </p>
                        </div></div>
                        </section>
                        
                        </body>
                        </html>
                        

                        When I searched for lamb, it found the expected instances in the lyrics section (class = “lyrics-text”), but surprisingly, it also found Lamb in the <title>. But the regex, as I originally wrote it, said it should only find matches after the string lyrics-text.

                        Did adding /G change the behavior I think you mentioned that it might make subsequent searches start at the beginning of the file.

                        1 Reply Last reply Reply Quote 0
                        • S
                          Sylvester Bullitt @Coises
                          last edited by Feb 5, 2024, 9:11 PM

                          @Coises Ships passing in the night.

                          I just made a post saying when I searched for the word lamb, it also found the word Lamb in the <title>, not just after lyrics-text. Search text:

                          <!DOCTYPE HTML>
                          <html lang="en-us">
                          
                          <head>
                          <meta charset="utf-8">
                          <title>Mary Had a Little Lamb</title>
                          <meta name="description" content="Words: Sarah Hale, 1830. Music: None.">
                          <meta name="keywords" content="Sarah Hale">
                          <link rel="stylesheet" href="../../css/hymn.css">
                          <script src="../../js/jquery.js"></script>
                          <script src="../../js/base.js"></script>
                          <script src="../../js/hymn.js"></script>
                          <link rel="prev" href="../../htm/h/e/w/o/hewonsav.htm">
                          <link rel="next" href="../../htm/h/e/s/a/hesallwo.htm">
                          <link rel="up" href="../../ttl/ttl-h.htm">
                          </head>
                          
                          <body>
                          
                          <section id="preface">
                          <h1 class="screen-reader-only">Introduction</h1>
                          <div class="preface-text">
                          <p><span class="lead">Words:</span> <a href="../../bio/h/a/l/e/hale_sjb.htm">Sarah J. Hale</a>, 1830.</p>
                          <p><span class="lead">Music:</span> John Doe  (<a href="../../mid/d/u/m/m/dummy.mid" title="Listen to music, MIDI format">🔊</a> <a href="../../pdf/en/d/u/m/m/Dummy.pdf" title="Download score, PDF format">pdf</a> <a href="../../nwc/d/u/m/m/Dummy.nwc" title="Download score, Noteworthy Composer format">nwc</a>).</p>
                          </div>
                          </section>
                          
                          <p>This page is used to test glo­bal search-and-replace us­ing re­gu­lar ex­pressions. </p>
                          
                          <section class="lyrics">
                          <div class="stanzas"><div class="lyrics-text mc ll">
                          <p>Mary had a little lambkin,<br>
                          Its fleece was white as snow.<br>
                          And everywhere that Mary went,<br>
                          The lamb was sure to go.<br>
                          He followed her to school one day,<br>
                          That was against the rule.<br>
                          It made the children laugh and play<br>
                          To see a lamb at school.</p>
                          <p>And so the teacher turned him out,<br>
                          But still he lingered near,<br>
                          And waited patiently about<br>
                          Till Mary did appear.<br>
                          And then he ran to her, and laid<br>
                          His head upon her arm,<br>
                          As if he said <q>I’m not afraid,<br>
                          You’ll keep me from all harm.</q></p>
                          <p><q>What makes the lamb love Mary so?</q><br>
                          The eager children cry.<br>
                          <q>‘Oh, Mary loves the lamb, you know,</q><br>
                          The teacher did reply.<br>
                          <q>And you each gentle animal<br>
                          In confidence may bind,<br>
                          And make them follow at your call,<br>
                          If you are always kind.</q> </p>
                          </div></div>
                          </section>
                          
                          </body>
                          </html>
                          

                          It sounds like your comment addresses that. Am I correct?

                          P 1 Reply Last reply Feb 5, 2024, 9:15 PM Reply Quote 0
                          • P
                            PeterJones @Sylvester Bullitt
                            last edited by PeterJones Feb 5, 2024, 9:16 PM Feb 5, 2024, 9:15 PM

                            @Sylvester-Bullitt said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                            Ships passing in the night.

                            More than that, you aren’t noticing all the posts because of the rapid posting.

                            I already explained exactly what happened with \G ion this post, which contains a fix for the \G issue.

                            @Coises’s follow-on showed that if any of your files don’t have lyrics-text at all, then my fix-for-\G will replace all instances of star or lamb or what have you – but I’m hoping, for your sake, that all the files that your Find in Files filter will match will contain lyrics-text somewhere.

                            C S 2 Replies Last reply Feb 5, 2024, 9:25 PM Reply Quote 0
                            • P
                              PeterJones @Sylvester Bullitt
                              last edited by Feb 5, 2024, 9:19 PM

                              @Sylvester-Bullitt said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                              (?:lyrics-text|\G)

                              (?:...) is a non-capturing subgroup .

                              S 1 Reply Last reply Feb 5, 2024, 9:28 PM Reply Quote 0
                              • C
                                Coises @PeterJones
                                last edited by Coises Feb 5, 2024, 9:28 PM Feb 5, 2024, 9:25 PM

                                @PeterJones said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                                @Sylvester-Bullitt said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                                Ships passing in the night.

                                More than that, you aren’t noticing all the posts because of the rapid posting.

                                I already explained exactly what happened with \G ion this post, which contains a fix for the \G issue.

                                @Coises’s follow-on showed that if any of your files don’t have lyrics-text at all, then my fix-for-\G will replace all instances of star or lamb or what have you – but I’m hoping, for your sake, that all the files that your Find in Files filter will match will contain lyrics-text somewhere.

                                Took me this long to get it (so I’ll post here rather than editing my earlier comment), but I think:

                                (?s)(\A.*?(lyrics-text|\Z(*COMMIT)(*FAIL))|\G).+?(?<!^)(?<!<p>)(?<!<p class="chorus">)\Kstar(?=(.+?</div>))
                                

                                fixes that problem.

                                1 Reply Last reply Reply Quote 1
                                • S
                                  Sylvester Bullitt @PeterJones
                                  last edited by Feb 5, 2024, 9:26 PM

                                  @PeterJones Three things:

                                  1. Yes, all the files (assuming they’re generated properly from my template) have the string lyrics-text.

                                  2. I just tried Coises’ suggested modification to the regex:

                                  (?s)(\A.*?lyrics-text|\G).+?(?<!^)(?<!<p>)(?<!<p class=“chorus”>)\Klamb(?=(.+?</div>))
                                  

                                  As advertised, it no longer matches the Lamb in the <title> tag, which is the desired behavior, since we’re only changing lyrics.

                                  1. What is the construct that resembles a lookbehind, but has the asterisk & question mark? That is,
                                  (\A.*?lyrics-text|\G)
                                  

                                  Still testing, but things are looking more and more promising!

                                  C P 2 Replies Last reply Feb 5, 2024, 9:44 PM Reply Quote 0
                                  • S
                                    Sylvester Bullitt @PeterJones
                                    last edited by Feb 5, 2024, 9:28 PM

                                    @PeterJones “I see,” said the blind carpenter, as he picked up his hammer and saw!

                                    C S 2 Replies Last reply Feb 5, 2024, 9:39 PM Reply Quote 0
                                    • C
                                      Coises @Sylvester Bullitt
                                      last edited by Feb 5, 2024, 9:39 PM

                                      @Sylvester-Bullitt

                                      Suggested reading:

                                      Perl Regular Expression Syntax
                                      Boost-Extended Format String Syntax

                                      Notepad++ uses the Boost regular expression library. The above links are to the documentation for the current version; I believe Notepad++ is a couple minor versions behind, but there should be little or no practical difference.

                                      1 Reply Last reply Reply Quote 0
                                      • C
                                        Coises @Sylvester Bullitt
                                        last edited by Feb 5, 2024, 9:44 PM

                                        @Sylvester-Bullitt said in Find-in-FIles: Can’t Replace Multiple Instances of Word:

                                        I just tried Coises’ suggested modification to the regex:

                                        That was @PeterJones, not me. I was in the process of writing a post explaining why it couldn’t be done when he posted showing how to do it.

                                        1 Reply Last reply Reply Quote 0
                                        • S
                                          Sylvester Bullitt @Sylvester Bullitt
                                          last edited by Feb 5, 2024, 9:45 PM

                                          @Sylvester-Bullitt Good news!

                                          Testing the new-and-improved regex against 2 files on disk worked perfectly!

                                          It even worked when I had to undo a mistake with the replacement string, changing it to the one I really meant (I just changed the regex and clicked Replace All again).

                                          So for now (fingers tightly crossed), it looks like we can declare victory! Does anyone have any more pearls of wisdom to add to this adventure?

                                          Thank you so much for for your help and patience.

                                          By the way, if you’d like to see the Web site where this will be used, click here !

                                          Cheers!

                                          C 1 Reply Last reply Feb 5, 2024, 10:15 PM Reply Quote 0
                                          27 out of 48
                                          • First post
                                            27/48
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors