regexp problem

Posted on 1999-11-15
Medium Priority
Last Modified: 2010-03-05
If I am converting text to html and want to replace spaces with &nbsp's, how would I write the reg exp IF I only want to do the replacement when there are more than 1 space:

1 space becomes 1 space
2 spaces becomes &nbsp&nbsp
3 spaces becomes &nbsp&nbsp&nbsp
Question by:red5
  • 2
LVL 19

Expert Comment

by:Kim Ryan
ID: 2209578
Note that \s means all white space (tab, CR etc) rather than just the space character.
$str =~ /\s{2,}/ and $str =~ s/ /&nbsp/g;

Author Comment

ID: 2209608
umm, didn't quite work...

this is my test:

$str = "A B C  D   E";
$str =~ /\s{2,}/ and $str =~ s/ /&nbsp/g;
print $str;



and I want:

A B C&nbsp&nbspD&nbsp&nbsp&nbspE
LVL 85

Accepted Solution

ozo earned 400 total points
ID: 2209701
$str =~ s/(\s{2,})/" "x length $1/eg;

Author Comment

ID: 2209710
that's it.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question