Community
    • Login

    Regex Find & Replace after Specific Text

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 2 Posters 2.7k Views 2 Watching
    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.
    • Chase RankinC Offline
      Chase Rankin
      last edited by PeterJones

      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

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP Offline
        PeterJones @Chase Rankin
        last edited by PeterJones

        @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\:

        Chase RankinC 1 Reply Last reply Reply Quote 1
        • Chase RankinC Offline
          Chase Rankin @PeterJones
          last edited by Chase Rankin

          @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

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP Offline
            PeterJones @Chase Rankin
            last edited by PeterJones

            @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.

            Chase RankinC 1 Reply Last reply Reply Quote 1
            • Chase RankinC Offline
              Chase Rankin @PeterJones
              last edited by

              @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.

              PeterJonesP 1 Reply Last reply Reply Quote 0
              • PeterJonesP Offline
                PeterJones @Chase Rankin
                last edited by PeterJones

                @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

                Chase RankinC 1 Reply Last reply Reply Quote 1
                • Chase RankinC Offline
                  Chase Rankin @PeterJones
                  last edited by

                  @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.

                  PeterJonesP 1 Reply Last reply Reply Quote 0
                  • PeterJonesP Offline
                    PeterJones @Chase Rankin
                    last edited by

                    @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.

                    Chase RankinC 1 Reply Last reply Reply Quote 1
                    • Chase RankinC Offline
                      Chase Rankin @PeterJones
                      last edited by

                      @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

                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                      With your input, this post could be even better 💗

                      Register Login
                      • First post
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors