Solved

PERL Force String Wrapping

Posted on 2007-03-23
9
916 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell script 9 81
convert Systemjs to Webpack 3 126
Need help Creating PowerShell Script 4 67
Hta File displays dynamic File names 4 65
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

734 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