Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Regex Find & Replace after Specific Text

    Help wanted · · · – – – · · ·
    2
    9
    125
    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 Rankin
      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

      PeterJones 1 Reply Last reply Reply Quote 0
      • PeterJones
        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 Rankin 1 Reply Last reply Reply Quote 1
        • Chase Rankin
          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

          PeterJones 1 Reply Last reply Reply Quote 0
          • PeterJones
            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 Rankin 1 Reply Last reply Reply Quote 1
            • Chase Rankin
              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.

              PeterJones 1 Reply Last reply Reply Quote 0
              • PeterJones
                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 Rankin 1 Reply Last reply Reply Quote 1
                • Chase Rankin
                  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.

                  PeterJones 1 Reply Last reply Reply Quote 0
                  • PeterJones
                    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 Rankin 1 Reply Last reply Reply Quote 1
                    • Chase Rankin
                      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
                      • First post
                        Last post
                      Copyright © 2014 NodeBB Forums | Contributors