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 1.5k Views 1 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.
    • PiotrMP006P Offline
      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 Offline
        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 Online
          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 Offline
            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

            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