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
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