How to add spaces at the beginning of a line in a block between lines in regular expression?
-
How to add spaces at the beginning of a line in a block between lines in regular expression?
ex
for aaa bbb ccc nextIt should look like this:
for aaa bbb ccc next -
for aaa bbb ccc nextwould 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
nspaces of indentation would just be(?-s)(?:\x20{n}.*)+Note that you could just use a literal space instead of
\x20, but\x20is preferred for readability reasons. Of course,\x20{n}could be replaced with\tif you are using tabs for indentation. -
@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…
-
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