• Login
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 808 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.
  • P
    PiotrMP006
    last edited by PiotrMP006 Jul 3, 2023, 2:42 PM Jul 3, 2023, 2:40 PM

    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
    
    M 1 Reply Last reply Jul 3, 2023, 2:58 PM Reply Quote 0
    • M
      Mark Olson @PiotrMP006
      last edited by Jul 3, 2023, 2:58 PM

      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.

      A 1 Reply Last reply Jul 3, 2023, 3:09 PM Reply Quote 0
      • A
        Alan Kilborn @Mark Olson
        last edited by Jul 3, 2023, 3:09 PM

        @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
        • M
          Mark Olson
          last edited by Jul 3, 2023, 3:27 PM

          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
          2 out of 4
          • First post
            2/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors