CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Consecutive characters

 



Melvosh
User

Nov 26, 2001, 9:13 AM

Post #1 of 9 (7442 views)
Consecutive characters Can't Post

I'm parsing (or attempting to parse) a textarea form submission. I'd like to replace all line breaks with
(which is working), and I'd also like to insert a
after 30 consecutive non-space characters, if they exist. I was given a suggestion, but without explanation, so I wasn't sure how to implement it. This is what I've got, but it's definitely not right. Any help would be appreciated. Thanks.

$new_issue = $ilmscript->param('Issue');
$new_issue =~ s/"^[^ ]{30}$"/g;
$new_issue =~ s/\r?\n/g;


"Outside of a dog, a book is man's best friend. Inside of a dog, it's too dark to read." - Grouch Marx

(This post was edited by Melvosh on Nov 26, 2001, 8:14 AM)


Coderifous
journeyman

Nov 26, 2001, 11:48 AM

Post #2 of 9 (7439 views)
Re: Consecutive characters [In reply to] Can't Post

OK, I'll try and help ya.
Here's the regex I think you need, and I'm assuming here that $text_area is going to be the variable containing the text from you input field.


Code
 
$text_area =~ s/(?:\r?\n|\s{30,})/\<br\>/g;



The regex is replacing all linereturn sequences, and all sets of spaces (\s) of 30 or more ({30,}) with the \<br\>.

I had to escape the br tag other wise it wouldn't pass through the HTML parser without getting interpreted, but you can take it out of the regex if you want. I don't think it will make a difference. Smile
Hope this helps.

--Jim



Melvosh
User

Nov 27, 2001, 4:53 AM

Post #3 of 9 (7431 views)
Re: Consecutive characters [In reply to] Can't Post

Sorry, I should've been clearer. I want to put in a \<br\> after every 30 consecutive non whitespace characters, rather than replace them with a \<br\>. Thought I'd let you know, the reg exp you posted does exactly what you specified.

"Outside of a dog, a book is man's best friend. Inside of a dog, it's too dark to read." - Grouch Marx


Coderifous
journeyman

Nov 27, 2001, 6:04 AM

Post #4 of 9 (7430 views)
Re: Consecutive characters [In reply to] Can't Post

Oops Blush I should have read the problem a little better. You wrote it fine, I just read it too fast. Try this:


#first replace the return sequences
$text_area =~ s/(\r?\n)/\<br\>/g;

#Now, if you want 30 or more Non-Whitespace
#characters to be matched then use this:
$text_area =~ s/(\S{30,})/$1\<br\>/g;



That should do it. Let me know if this helps!

--Jim



Melvosh
User

Nov 27, 2001, 7:17 AM

Post #5 of 9 (7428 views)
Re: Consecutive characters [In reply to] Can't Post

Hrm...That really looked like it should work. Unfortunately, it didn't. The line breaks are being replaced just fine, but it's not inserting the \<br\> after 30 characters. I tried it without the expression to replace the line breaks, and it still didn't work. The only thing that's different is the variable name. Any ideas? Thanks.

"Outside of a dog, a book is man's best friend. Inside of a dog, it's too dark to read." - Grouch Marx


Coderifous
journeyman

Nov 27, 2001, 10:10 AM

Post #6 of 9 (7425 views)
Re: Consecutive characters [In reply to] Can't Post

 
I don't know why it's not working other than maybe there isn't an instance where 30 non-whitespace characters occur without at least one occurence of white space. Make sure that you don't have the ?: after the opening paren's in the regex, that would cause it to not store the match in $1. Hmmm what could it be... can you post an example of the text you are parsing? Maybe that would help me.

--Jim



Melvosh
User

Nov 27, 2001, 12:03 PM

Post #7 of 9 (7421 views)
Re: Consecutive characters [In reply to] Can't Post

This is an example
of the text that I am
trying to parse.
Wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeee

It returns as this

This is an example
of the text that I am
trying to parse.
Wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeee

I'm not sure what exactly it's doing, but it doesn't appear to be inserting a \<br\> after 30 characters. The only reason it is being wrapped at all is because of the spaces.

"Outside of a dog, a book is man's best friend. Inside of a dog, it's too dark to read." - Grouch Marx


thodi
stranger

Nov 28, 2001, 1:21 AM

Post #8 of 9 (7416 views)
Re: Consecutive characters [In reply to] Can't Post

You may want to take a look at Text::Wrap: http://search.cpan.org/doc/MUIR/Text-Tabs+Wrap-2001.0131/lib/Text/Wrap.pm. This module is capable of doing exactly what you want.



Melvosh
User

Nov 28, 2001, 4:53 AM

Post #9 of 9 (7413 views)
Re: Consecutive characters [In reply to] Can't Post

Got it working. All I had to do was take out the , and it worked great. It was matching against 30 or more characters, rather than just 30 characters. Thanks for all of your help. I really appreciate it.

"Outside of a dog, a book is man's best friend. Inside of a dog, it's too dark to read." - Grouch Marx

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives