Solved

PERL Force String Wrapping

Posted on 2007-03-23
9
913 Views
Last Modified: 2013-12-25
I am having a problem with a string. I have a field that I let users type in free text. Some users don't use spaces.. i.e. sep everything with commas instead of spaces. When they do this and I display the content is messes up my page format. Is there a way to force a space or <br> tag every X characters using regex?
0
Comment
Question by:Bob-Villa
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:midrisi
ID: 18781036
This may resolve your problem.I am replacing all commas with one single space.

#!/usr/local/bin/perl

$text="bangalore,new york,london                                     cunnighma,dehli";
$text=~ s/,/ /g;

print $text;
0
 
LVL 1

Author Comment

by:Bob-Villa
ID: 18781109
I don't mind the commas and I don't want to replace every comma. adding a space after every consecutive length of $x characters would be the best solution for me.
0
 
LVL 8

Expert Comment

by:nedfine
ID: 18781703
insert space  after 5 characters
$x=5;  #change this to any value
@a = split //;
for($i=0;$i<$#a;$i++)
{
push($a[$i],@b);
$j = $i % $x;
if($j == 0)
{
push(" ",@b);
}
}
print "\n @b";
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Expert Comment

by:nedfine
ID: 18781726
sorry thers a mistake....

$x=5;  #change this to any value
@a = split //;
for($i=0;$i<$#a;$i++)
{
push(@b,$a[$i]);
$j = $i % $x;
if($j == 0)
{
push(@b," ");
}
}
print "\n @b";
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 125 total points
ID: 18781747
Adding a space after every consecutive length of $x characters would pretty much guarantee that it would add a space where it's not supposed to, such as in the middle of a word.

How about adjusting midrisi's suggestion and add a space after every comma?

$text=~ s/,/, /g;
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 18781795
If you really want to add the space at every x length, then a simple regex would suffice.

$text="bangalore,new york,london,cunnighma,dehli";

$x = 5;
$text =~ s/\G(.{$x})/$1 /g;
print $text;

----

outputs:
banga lore, new y ork,l ondon ,cunn ighma ,dehl i
0
 
LVL 84

Expert Comment

by:ozo
ID: 18781914
How about adding <wbr /> after $x non spaces followed by comma?
s#(\S{$x},)(?!\s)#$1<wbr />#g
0
 
LVL 1

Author Comment

by:Bob-Villa
ID: 18782374
FishMonger's  $text=~ s/,/, /g; is great (can't believe I didn't think of that) but the only problem is with numbers (i.e. 3,000) I will almost never have part of the string have a 2 numbers sep by a comma unless it is a number greater than 999. I know this is sloppy but it will solve 99% of my issues.

sample string:
THISIS3,000,abcdef

THISIS3,000, abcdef (OK - this would be perfect)
THISIS3, 000, abcdef (NOT OK)

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 18783257
$text=~ s/,(?!\d\d\d)/, /g;
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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