Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regexp to replace some number of characters?

0 views
Skip to first unread message

david...@gmail.com

unread,
May 22, 2008, 4:49:13 AM5/22/08
to
Greetings.

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!


Mirco Wahab

unread,
May 22, 2008, 5:18:31 AM5/22/08
to

This would then be like:
...
$inputdata =~ s/^(\x20+)/' 'x length($1)/mge;
..

another one would be:

...
1 while $inputdata =~ s/^\x20|(?<=\&nbsp;)\x20/\&nbsp;/mg;
...

Which is nicer? I don't know.

Regards

M.

Peter Makholm

unread,
May 22, 2008, 6:08:00 AM5/22/08
to
Mirco Wahab <wa...@chemie.uni-halle.de> writes:

> This would then be like:
> ...
> $inputdata =~ s/^(\x20+)/'&nbsp;'x length($1)/mge;
> ..

I would do like this.

> another one would be:
>
> ...
> 1 while $inputdata =~ s/^\x20|(?<=\&nbsp;)\x20/\&nbsp;/mg;
> ...

Breaks if you have '&nbsp; ' other places in you line for some
reason. I would work from the other end:

1 while $inputdata =~ s/^(\x20*)\x20/$1&nbsp;/;

//Makholm

Dr.Ruud

unread,
May 22, 2008, 11:05:05 AM5/22/08
to
david...@gmail.com schreef:

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+)/'&nbsp;'x length($1)/mge;

to also replace other whitespace variants than " ".

--
Affijn, Ruud

"Gewoon is een tijger."

0 new messages