Community
    • Login

    How to add spaces at the beginning of a line in a block between lines in regular expression?

    Scheduled Pinned Locked Moved General Discussion
    4 Posts 3 Posters 776 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.
    • PiotrMP006P
      PiotrMP006
      last edited by PiotrMP006

      How to add spaces at the beginning of a line in a block between lines in regular expression?

      ex

      for
      aaa
      bbb
      ccc
      next
      

      It should look like this:

      for
        aaa
        bbb
        ccc
      next
      
      Mark OlsonM 1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson @PiotrMP006
        last edited by

        for
          aaa
          bbb
          ccc
        next
        

        would be matched by the regular expression ^for\R\x20{2}aaa\R\x20{2}bbb\R\x20{2}ccc\Rnext$

        More generally a block with at least n spaces of indentation would just be (?-s)(?:\x20{n}.*)+

        Note that you could just use a literal space instead of \x20, but \x20 is preferred for readability reasons. Of course, \x20{n} could be replaced with \t if you are using tabs for indentation.

        Alan KilbornA 1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Mark Olson
          last edited by

          @Mark-Olson said in How to add spaces at the beginning of a line in a block between lines in regular expression?:

          would be matched by the regular expression

          I’m confused by this response because it solves a problem that wasn’t asked…

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

            Yep, AlanKilborn is right, I misunderstood your question.

            Assuming you’re trying to use a find/replace to convert the first example to the second example, I recommend

            FIND: (?s-i)(?:^for\R|(?!\A)\G)(?:(?!\Rnext$).)*?\K^
            REPLACE WITH \x20\x20

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