substitute two ore more with two or more

R

Robert Wallace

I want to find two or more spaces and replace them with the same number
found.
I there are three items found, replace them with three other items. four
for four, etc...

If I have:
$x="<img src='pic.gif'> a b c d";
and I want to output:
<img
src='pic.gif'>&nbsp;a&nbsp;&nbsp;b&nbsp;&nbsp;&nbsp;c&nbsp;&nbsp;&nbsp;&nbsp;d

I thought this would work:
$x=~s/ {2,}/&nbsp;/g;
but that replaces all two or more spaces with one &nbsp;

these didn't work
$x=~s/ {2,}/$1/g;
$x=~s/ {2,}/(($1)x3)/g;
 
B

Big and Blue

Robert said:
for four, etc...

If I have:
$x="<img src='pic.gif'> a b c d";
and I want to output:
<img
src='pic.gif'>&nbsp;a&nbsp;&nbsp;b&nbsp;&nbsp;&nbsp;c&nbsp;&nbsp;&nbsp;&nbsp;d

I thought this would work:
$x=~s/ {2,}/&nbsp;/g;
but that replaces all two or more spaces with one &nbsp;
...

$x=~s/( {2,})/"&nbsp;" x length($1)/eg;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top