Solved

PERL Force String Wrapping

Posted on 2007-03-23
9
911 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
 
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
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 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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Register AutoHotkey 12 55
Using a variable to name output in powershell script 2 29
Sums of coloumns in html/java 15 67
Need script to search multiple files in one drive 13 35
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now