?
Solved

regexp problem

Posted on 1999-11-15
4
Medium Priority
?
185 Views
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
etc
0
Comment
Question by:red5
  • 2
4 Comments
 
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;
0
 

Author Comment

by:red5
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;

yields

A&nbspB&nbspC&nbsp&nbspD&nbsp&nbsp&nbspE

and I want:

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

Accepted Solution

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

Author Comment

by:red5
ID: 2209710
that's it.
0

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