Can’t figure out how to build replacement pattern for the following.
I have an array of lines like the one below:
<span> code <span> code <span/> code </span>
<span> code <span> code <span/> code </span>
<span> code <span> code <span/> code </span>
<span> code <span> code <span/> code </span>
There is a space at the start of each line. It could be one space, or two spaces or N spaces.
Also there could be spaces anywhere in the line itself.
What’s required, is to substitute spaces only at the start of the line before first < character.
Each space should be substituted to
So if there is 1 space character – it should be substituted to if there 2 spaces – should be Basically, N space characters should be substituted to N
I can do it with code for sure, but it’s not optimal.
I’ve tried to do this purely with regex, but can’t figure out how to build proper pattern.
Examples of this replacement on any language (Ruby, C#, Python, Perl) are welcome.
P.S. I’m processing line-by-line, so it is enough to build replacement pattern, which will work within boundaries of a single line.
P.P.S. I was close enough with lookaheads, but still can’t figure out how to stop replacement of spaces after first <.
In c# this is no problem
Means replace a whitespace character, but only if there are only whitespace characters and the start of the string before.
But all the other languages does not support variable length lookbehind.