• Login
Community
  • Login

Regex Find & Replace after Specific Text

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 2 Posters 1.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.
  • C
    Chase Rankin
    last edited by PeterJones Oct 18, 2022, 4:14 PM Oct 18, 2022, 3:53 PM

    Basically. I have a bunch of text files that someone incorrectly plugged a value into roughly 650 files. The + was supposed to be a - and vice versa. I am doing my best to come up with a way to mass edit these. There is alot more information in these documents other wise I would just find and replace the +/- as needed.

    The closest I got: /^.*Pattern:.*(?:\+|-)$

    This is roughly what the files look like.

    //extra crap
    //c:/imessedup/i-don't-know
    //HHLAU_MD01_HIGHFIVES
    
    //*****INFORMATION ABOUT MODULATION***
    
    
    Pattern:            +-+++---++--++--+++
    

    –
    moderator added code tags to make it readable

    P 1 Reply Last reply Oct 18, 2022, 4:25 PM Reply Quote 0
    • P
      PeterJones @Chase Rankin
      last edited by PeterJones Oct 18, 2022, 4:27 PM Oct 18, 2022, 4:25 PM

      @Chase-Rankin ,

      Going to the Generic Regex formula FAQ, and going to the Replacing in a specific zone, I went to the simplified formula for a single-line zone:
      b06ef136-a414-4619-b0e3-6abe7729ab38-image.png

      I then translated that into a three-regular-expression sequence.

      1. Pluses to smilies:
        SEARCH (?-s)(?-i:^Pattern\:|(?!\A)\G).*?\K(?-i:\+)
        REPLACE ☺

      2. Minuses to pluses:
        SEARCH (?-s)(?-i:^Pattern\:|(?!\A)\G).*?\K(?-i:\-)
        REPLACE +

      3. Smilies to minuses:
        SEARCH (?-s)(?-i:^Pattern\:|(?!\A)\G).*?\K(?-i:☺)
        REPLACE -

      (Mode = regular expression for all)

      This will only do replacements on lines that start with Pattern:
      If you actually have spaces or tabs before Pattern, then change the middle portion to ^\h*Pattern\:

      C 1 Reply Last reply Oct 18, 2022, 4:44 PM Reply Quote 1
      • C
        Chase Rankin @PeterJones
        last edited by Chase Rankin Oct 18, 2022, 4:57 PM Oct 18, 2022, 4:44 PM

        @PeterJones First off, thank you for taking the time to respond. I’m looking at this in regex101 and it seems to work there but when I took it to Notepad++(transcribed to stand-alone system) it isn’t working. I’ve quintuple checked character for character. I am using a version of Notepad++ from 2018 though. Also, have tried this with . matches newline and without

        P 1 Reply Last reply Oct 18, 2022, 5:01 PM Reply Quote 0
        • P
          PeterJones @Chase Rankin
          last edited by PeterJones Oct 18, 2022, 5:03 PM Oct 18, 2022, 5:01 PM

          @Chase-Rankin ,

          It worked just fine for me.

          But something I didn’t emphasize in my post, even though it was mentioned in the “Replacing in a specific zone” documentation, you must use Replace All: if there is a \K in the Find what: regex, you have to use Replace All.

          The .-matches-newline is irrelevant, because the expression has (?-s) which overrides that checkbox.

          C 1 Reply Last reply Oct 18, 2022, 5:18 PM Reply Quote 1
          • C
            Chase Rankin @PeterJones
            last edited by Oct 18, 2022, 5:18 PM

            @PeterJones I’m going to download the newest version and try it again. Also will copy over your text verbatim so I don’t mess anything up.

            P 1 Reply Last reply Oct 18, 2022, 5:35 PM Reply Quote 0
            • P
              PeterJones @Chase Rankin
              last edited by PeterJones Dec 16, 2024, 3:54 PM Oct 18, 2022, 5:35 PM

              @Chase-Rankin ,

              Ah, yes, v7.9.1 fixed a bug in \A , which may have been making the regex not work right. I tried in v7.7.1, and it only replaced the first plus with the smiley, whereas in v7.9.5 (soon after the fix), it worked as I originally described.

              This requirement was actually noted in the IMPORTANT notes in the “Replacing in a region” FAQ

              C 1 Reply Last reply Oct 18, 2022, 5:44 PM Reply Quote 1
              • C
                Chase Rankin @PeterJones
                last edited by Oct 18, 2022, 5:44 PM

                @PeterJones Ok. I messed up when I told you Pattern:. It was actually all caps. Now that is corrected but when I click replace all it is only going one character at a time.

                P 1 Reply Last reply Oct 18, 2022, 5:56 PM Reply Quote 0
                • P
                  PeterJones @Chase Rankin
                  last edited by Oct 18, 2022, 5:56 PM

                  @Chase-Rankin said in Regex Find & Replace after Specific Text:

                  Now that is corrected but when I click replace all it is only going one character at a time.

                  That happens if you are on a version before v7.9.1, because of the bug in \A. For this expression to work, you need to be on v7.9.1 or newer.

                  C 1 Reply Last reply Oct 18, 2022, 6:48 PM Reply Quote 1
                  • C
                    Chase Rankin @PeterJones
                    last edited by Oct 18, 2022, 6:48 PM

                    @PeterJones Thanks so much for all the help. I got it done with the old version by just force clicking through it all. Pressing my IT dept to install the newest version. Thanks again!

                    1 Reply Last reply Reply Quote 1
                    3 out of 9
                    • First post
                      3/9
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors