I have some input data that looks something like this:
A NON-INDENTED LINE
THIS LINE INDENTED THREE SPACES
The number of spaces that some lines are indented will vary.
I want to replace each leading space with a HTML-ish ' ' so that
the second line becomes
' THIS LINE...'
I can think of ugly ways to do this (like matching the whitespaces and
then substituting ' ' x length $whitespaces). But I would prefer
to learn a more elegant solution.
Cheers!
This would then be like:
...
$inputdata =~ s/^(\x20+)/' 'x length($1)/mge;
..
another one would be:
...
1 while $inputdata =~ s/^\x20|(?<=\ )\x20/\ /mg;
...
Which is nicer? I don't know.
Regards
M.
> This would then be like:
> ...
> $inputdata =~ s/^(\x20+)/' 'x length($1)/mge;
> ..
I would do like this.
> another one would be:
>
> ...
> 1 while $inputdata =~ s/^\x20|(?<=\ )\x20/\ /mg;
> ...
Breaks if you have ' ' other places in you line for some
reason. I would work from the other end:
1 while $inputdata =~ s/^(\x20*)\x20/$1 /;
//Makholm
It can be done with a regex, because Perl allows code inside a regex,
but normally one would use the substitution operator s///.
I prefer the first solution of Mirco, but would write it like this:
$inputdata =~ s/^(\s+)/' 'x length($1)/mge;
to also replace other whitespace variants than " ".
--
Affijn, Ruud
"Gewoon is een tijger."