Forum Moderators: coopster & phranque

Message Too Old, No Replies

Need a Perl Regular Expression to break long lines of text

         

Donboy

12:20 am on Apr 21, 2003 (gmt 0)

10+ Year Member



What I want to do is prevent somebody from going to my form, pressing the same letter key on their keyboard and holding it down (making a whole bunch of the same character). When they do this and submit the form, when the form's contents are displayed as HTML, there are no line breaks.

I found a regular expression that is supposed to do this, but it's not working.

$body =~ s/.{,70} /$&\n /sg;

This is supposed to insert a new line after 70 occurrances of the same character, but it's not working for me. Can somebody help?

ShawnR

4:23 am on Apr 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change {,70} to {70}

The second problem is that it won't match the same character 70 times, it will match any 70 characters. But anyway, you don't really want to match the same character 70 times. You want to match any contiguous non-whitespace chars, so try:

\S{70} instead of .{,70}

Shawn

Donboy

4:44 am on Apr 21, 2003 (gmt 0)

10+ Year Member



Ok, so that would give me this, right?

$body =~ s/\S{70}/$&<br>/sg;

NOTE: I changed the newline into a BR since I wanted to make sure the outputted HTML would have a newline.

I just tried using this, but it didn't work. There must be something I'm doing wrong. HELP!

ShawnR

5:04 am on Apr 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, $body =~ s/\S{70}/$&<br>/sg; looks OK to me.

Could you provide further details, such as:

  • The symptoms (what does happen?)
  • The results of some debugging (e.g. print $body; before and after the regex statement. You should see the <br>'s)
  • The context (e.g. further details about how the html is sending the form, how your script is receiving it, and how your result page is generated)

If its too big to post, feel free to email/sticky me, and we'll post back the solution later.

Shawn

Donboy

6:11 am on Apr 21, 2003 (gmt 0)

10+ Year Member



Absolutely nothing happens. Here's what is located around the regex...

$body =~ s/\cM//g;
$body =~ s/\n\n/<p>/g;
$body =~ s/\n/<br>/g;
$body =~ s/ /          /g;
$body =~ s/\¦/\&#124\;/g; #escape pipes
$body =~ s/\S{70}/$&<br>/sg;

After this happens, the $body variable is written into a pipe delimited file along with several other variables. Once this happens, the file is read and the contents are displayed as HTML for the user to see. Opening the delimited file, I can find no br's in there, so it looks like nothing happened. All I saw was a whole bunch of the same character in one long string.

Since no good solution seems clear, maybe I will message you privately.

Donboy

12:45 am on Apr 24, 2003 (gmt 0)

10+ Year Member



To anyone doing a search or looking for answers to this problem, Shawn helped me find the solution....

$body =~ s/\S{70}/$&<br>/sg;

The earlier post had some spaces in the code but they didn't show up in the post, so by sending the file over email he saw my error.