I am looking for a regex substitution to transform N white spaces at the beginning of a line to N
. So this text:
list:
- first
should become:
list:
- first
I have tried:
str = "list:\n - first"
str.gsub(/(?<=^) */, " ")
which returns:
list:
- first
which is missing one
. How to improve the substitution to get the desired output?
CodePudding user response:
You could make use of the \G
anchor and \K
to reset the starting point of the reported match.
To match all leading single spaces:
(?:\R\K|\G)
(?:
Non capture group\R\K
Match a newline and clear the match buffer|
Or\G
Assert the position at the end of the previous match
)
Close non capture group and match a space
See a regex demo and a Ruby demo.
To match only the single leading spaces in the example string:
(?:^.*:\R|\G)\K
In parts, the pattern matches:
(?:
Non capture group^.*:\R
Match a line that ends with:
and match a newline|
Or\G
Assert the position at the end of the previous match, or at the start of the string
)
Close non capture group\K
Forget what is matched so far and match a space
See a regex demo and a Ruby demo.
Example
re = /(?:^.*:\R|\G)\K /
str = 'list:
- first'
result = str.gsub(re, ' ')
puts result
Output
list:
- first
CodePudding user response:
Use gsub
with a callback function:
str = "list:\n - first"
output = str.gsub(/(?<=^|\n)[ ] /) {|m| m.gsub(" ", " ") }
This prints:
list:
- first
The pattern (?<=^|\n)[ ]
captures one or more spaces at the start of a line. This match then gets passed to the callback, which replaces each space, one at a time, with
.