Community
    • Login

    Find and Replace between {}

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 3 Posters 379 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.
    • M
      masterofdisaster
      last edited by masterofdisaster

      Hey Guys,

      do anyone of you got an Idea…
      I need to replace every “.” to " \ " if its in {}… for this example:
      This is a example.text:text.text
      More{example.text}

      After all
      This is a example.text:text.text
      More{example\text}
      These are tested by myself:
      64e4e722-570e-488a-ad3c-604c623a9c20-grafik.png

      Thanks in advance!

      1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson
        last edited by

        In the future, please use
        ```
        ```
        to enclose blocks of example text, rather than using an image. That way other users can copy your example text and paste it into Notepad++.

        In any case, a reasonable way to replace . with \ if it is inside {} is to open the find/replace form (Ctrl+H with default keybindings) and use the following fields:
        Find what: (?s)(?:\{|(?!\A)\G)[^{}]*?\K(?:[^}]*(?:\Z|(?=\{))(*SKIP)(*FAIL)|\.)
        Replace with: \
        Search Mode: Regular expression

        You didn’t specify whether it was acceptable for there to be any { in between {}, so I assumed that no { or } could be between the {}. You also didn’t specify whether the opening { and the closing } could be on separate lines. If you want to tweak the regex because I was wrong about your requirements, you are free to do that, but I won’t do it for you because making those changes could be a good learning experience for you.


        This regular expression makes use of the SKIP and FAIL backtracking control verbs and the generic regex for searching in a region of text. Most of the complexity comes from the need to ignore . characters following a { that does not have a matching }.


        This regex-replace converts

        { foo.txt {
        {foo.txt}
        {foo.bar.baz.txt rejkrje.kj rjrj.rj}
        {foo.
            bar.] }
        foo . . .
           . bar ]{
            . .
            . . {
            . .
        

        to

        { foo.txt {
        {foo\txt}
        {foo\bar\baz\txt rejkrje\kj rjrj\rj}
        {foo\
            bar\] }
        foo . . .
           . bar ]{
            . .
            . . {
            . .
        
        1 Reply Last reply Reply Quote 0
        • M
          masterofdisaster
          last edited by masterofdisaster

          Hey @Mark-Olson !

          First, I would like to thank you very much! You have helped me a lot, and you are absolutely right. Next time, I will describe my specifications more precisely and not include images!

          I think this topic can be closed.
          Thank you very much!

          Mark OlsonM 1 Reply Last reply Reply Quote 0
          • Mark OlsonM
            Mark Olson @masterofdisaster
            last edited by

            @masterofdisaster said in Find and Replace between {}:

            I think this topic can be closed.

            I’m glad I could help! “Closing” topics is not a thing here though.

            In any case, I’d like to revise my regex-replace a bit, and change it to:
            Find what: (?:\{(?=[^{}]*\})|(?!\A)\G)[^{}]*?\K\.
            Replace with: \
            Search Mode: Regular expression

            The only difference from my previous recommendation is the Find what field. I was rushing when I came up with my previous Find what regex, and I missed a scenario where it could exhibit catastrophically bad performance, namely the case of { followed by very many (say, 2000 or more) . characters, followed by }, which my old regex-replace would match correctly, but taking time proportional to the square of the length of the document.

            This issue illustrates the importance of thinking carefully when making regular expressions, especially ones using lookahead and lookbehind; it is very easy to open yourself up to catastrophic backtracking. Of course, it also illustrates the power of those more advanced regex capabilities; the much weaker engine of (for example) VSCode could not do this regex-replace.

            1 Reply Last reply Reply Quote 0
            • guy038G
              guy038
              last edited by

              Hello, @masterofdisaster, @mark-olson and All,

              Hi, Mark, could you verify that the regex S/R, below, seems to be the minimum one to get the expected results ?

              I personally tested it with about 12,100 characters between a { and a } boundaries, and with or without dot characters inside this range !

              SEARCH (?:\{|(?!\A)\G)[^}]*?\K\.

              REPLACE \

              Best Regards,

              guy038

              Mark OlsonM 1 Reply Last reply Reply Quote 0
              • Mark OlsonM
                Mark Olson @guy038
                last edited by Mark Olson

                @guy038
                Without even testing it, I agree that (?:\{|(?!\A)\G)[^}]*?\K\. will not have the scaling issues, but it does not correctly handle the edge case of {. or any other document with { followed by . without the matching } afterwards.

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