Forum OpenACS Q&A: Users doing Lines, Gotta Stop 'em REGSUB

Request notifications

Some of our Forum Users Like to separate stuff in their forum posts with:

===========
or
~~~~~~~~~~~~
or
____________

Usually they get carried away and make their lines too long which gets presented as a LONG word which widens the <table... column to get wider than the fixed column width. It makes horizontal scroll bars appear at the bottom of the browser and makes reading the forum thread a PAIN.

You know you often can't reason with users, so my solution is to program around 'em. So, I've put in a couple of Regexp like this to make the lines break into smaller "word" segments:

regsub -all "~~~~~~~" $responses "~~~~~~~ " responses
regsub -all "=======" $responses "======= " responses
Now I'm stumped with two new long line variations. I cannot use "+++++" or "*****" because regsub barfs, evidentally because these are reserved characters. Even using a backslash in front of each + or * doesn't work. So, my Question: How do I make the Lonnnng lines break with spaces every 20 characters with + and *.
OR
is there a more elegant and cool regsub or regexp that would take ANY "word" longer 20 characters and just add a SPACE so that the tables within the browsers don't freak out?

TIA -Bob

Collapse
Posted by Tilmann Singer on
Quote from some tcl doc: backslashes in regexps tend to interact badly with tcl string handling, so you have to enclose your regexps in { } to avoid substitution. Double backslashes might work as well.
Doubling backslashes will definitely work.  Tcl will see backslashbackslash and escape it to a single backslash which will then get passed off to the regular expression engine which will use it to escape the next character (the + or the * in this case).
Collapse
Posted by Tom Jackson on

You should probably set a var equal to the string you want to replace, and the replacement:

set reg {some_reg} 
set replacement {some_replacement}
regsub -all $reg $responses $replacement responses

Don't use quotes around the value, use curlys. Same goes for string match. The pattern should be in curlys, not quotes, or set beforehand.