Paulie Posted July 23, 2008 Posted July 23, 2008 I'm kind of anxious to know the answer to this.. I've tried everything i can think of... WHERE ARE YOU REGEXP GODS?! muttley
GEOSoft Posted July 24, 2008 Posted July 24, 2008 (edited) I'm kind of anxious to know the answer to this.. I've tried everything i can think of...WHERE ARE YOU REGEXP GODS?! muttleyYou may have to wait a while Paulie. Smoke_N doesn't have much time for the forums these days. He does still check but not as frequently as he once did.Edit: Paulie, did you try replacing \r with \r\n|\z Edited July 24, 2008 by GEOSoft George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
Moderators SmOke_N Posted July 24, 2008 Moderators Posted July 24, 2008 (edited) I read the thread, but I'm still lost as to what exactly the goal is here (Step by Step).I do better with step by step, then and a Before and After, so I don't waste my time. Edited July 24, 2008 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Champak Posted July 24, 2008 Author Posted July 24, 2008 (edited) The last thing that needs to be done with this is, forgive me if I'm not clear, if there is a punctuation at the end of the word, it should be considered as part of the word. Ex. "John jumped." The period in that is considered as part of "jumped", so if the period lands at the wrap point, then the entire word along with the period, "jumped." is wrapped to the next line and not just the period. I guess this can be accomplished with REGEXP. Edited July 24, 2008 by Champak
Moderators SmOke_N Posted July 24, 2008 Moderators Posted July 24, 2008 (edited) Ok, so I read through everything, this didn't take but about a minute, so if it's wrong $Text = "THIS IS A REALLY LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _ " AND SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!" $CharsPerLine = 15 $s_output = StringRegExpReplace($Text, "(.{" & $CharsPerLine & "}\w*[\s,\?\.\!'" & '"]*)', "\1" & @CRLF) MsgBox(0, @extended, $s_output) Edit: You may want to throw in [[:punct:]] in there or manually add all the punctuation marks. Edited July 24, 2008 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Champak Posted July 24, 2008 Author Posted July 24, 2008 That's cool, as of now I can't see a problem with it for me, but technically, what that does is...I guess...is prevent the wrap if the punctuation is the last thing, or something like that. The problem that may arise out of that in some cases is if someone does something like triple/quadruple or some other type of punctuation combo, it just stretches out the msgbox and defeats the purpose of the wrap at that point until the next word. As I said, I'm pretty much good with it as is, but thought I'd just let you know. Thanks.
Moderators SmOke_N Posted July 24, 2008 Moderators Posted July 24, 2008 That's cool, as of now I can't see a problem with it for me, but technically, what that does is...I guess...is prevent the wrap if the punctuation is the last thing, or something like that. The problem that may arise out of that in some cases is if someone does something like triple/quadruple or some other type of punctuation combo, it just stretches out the msgbox and defeats the purpose of the wrap at that point until the next word. As I said, I'm pretty much good with it as is, but thought I'd just let you know. Thanks.Err... I'm not sure I'm following you. Do you even understand what it does? It doesn't just stop at one punctuation if there are more. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Paulie Posted July 24, 2008 Posted July 24, 2008 (edited) ...this didn't take but about a minute...ppsh... yeah ok.. show off! EDIT: i think he means the point is to limit the max length of each line it there are a whole punch of punct marks, it doesn't wrap word+Punctuation to the next line. Observe: #include <array.au3> $Text = "THIS IS A REALLY LONG. TEXT LINE THAT. WOULD MAKE A REALLY. LONG. MESSAGE. BOX." & _ " AND.!!!! SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!" $CharsPerLine = 9 $s_output = StringRegExp($Text, "(.{" & $CharsPerLine & "}\w*[\s,\?\.\!'" & '"]*)',3) _ArrayDisplay($S_Output) Dim $Lengths[UBound($S_Output)] For $i = 0 to UBound($S_Output)-1 $Lengths[$i] = StringLen($S_Output[$i]) Next _ArrayDisplay($Lengths, "MAX LENGTH: "&$CharsPerLine) Edited July 24, 2008 by Paulie
Champak Posted July 24, 2008 Author Posted July 24, 2008 (edited) Take this for example $Text = "THIS IS A REALLY LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _ " AND SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!!!!!!!!!!!!!!!!!!!!!!!!!. Another line." $CharsPerLine = 23 $s_output = StringRegExpReplace($Text, "(.{" & $CharsPerLine & "}\w*[\s,\?\.\!'" & '"]*)', "\1" & @CRLF) MsgBox(0, @extended, $s_output) My thinking BEFORE, was that it should wrap at "LONG", because the exclamation points (although extreme in this example) are pushing the msgbox beyond the size the programmers design/wishes; the punctuations should be considered as part of the word, and hence cause the word "LONG" to go to the next line. But as I said I think it is great as is, and can now see reasons why it should prob stay this way...or maybe have an option if anything. Edited July 24, 2008 by Champak
Moderators SmOke_N Posted July 24, 2008 Moderators Posted July 24, 2008 Take this for example $Text = "THIS IS A REALLY LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _ " AND SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!!!!!!!!!!!!!!!!!!!!!!!!!. Another line." $CharsPerLine = 23 $s_output = StringRegExpReplace($Text, "(.{" & $CharsPerLine & "}\w*[\s,\?\.\!'" & '"]*)', "\1" & @CRLF) MsgBox(0, @extended, $s_output) My thinking BEFORE, was that it should wrap at "LONG", because the exclamation points (although extreme in this example) are pushing the msgbox beyond the size the programmers design/wishes; the punctuations should be considered as part of the word, and hence cause the word "LONG" to go to the next line. But as I said I think it is great as is, and can now see reasons why it should prob stay this way...or maybe have an option if anything.Well word wrap isn't going to stop at an exact number. What if you have a word that is more than the characters you have allotted for? That's why I wrote it like I did. It's up to you to use a sensible max buffer to understand that there may be situations that could arise that you have a 50 character word, and using 23 characters as a buffer just doesn't seem to make much sense does it. Either that, or you can go back to the way ya'll were doing it before ... Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Champak Posted July 24, 2008 Author Posted July 24, 2008 (edited) That's exactly why I said.and can now see reasons why it should prob stay this way...I agree. Edited July 24, 2008 by Champak
Moderators SmOke_N Posted July 24, 2008 Moderators Posted July 24, 2008 (edited) That's exactly why I said. I agree.I didn't think I read that... anyway, you have my curiousity up now... is this more what you expected:$Text = "THIS IS A REALLY LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _ " AND SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!!!!!!!!!!!!!!!!!!!!!!!!!. Another line." $CharsPerLine = 23 $s_output = StringRegExpReplace($Text, "(.{" & $CharsPerLine & "}\w*[\s,;:></\\|\}\]\{\[-_\?\.\!'" & '"]*)', "\1" & @CRLF) $s_output2 = _StringRegExpWordWrap($Text, $CharsPerLine) MsgBox(0, @extended, $s_output & @CRLF & @CRLF & $s_output2) Func _StringRegExpWordWrap($s_string, $i_maxchars) Local $s_output = StringRegExpReplace($s_string, _ "([\w\s,;:></\\|\}\]\{\[-_\?\.\!'" & '"]{' & $i_maxchars & "}\w*)", "\1" & @CRLF) Return StringRegExpReplace($s_output, "\r\n\z", "") EndFunc(The output2)? Edit: The first one I did there with no function is a better way to do it I think... because the second one won't just pick up "any char", only the ones I specified. Edited July 24, 2008 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Champak Posted July 24, 2008 Author Posted July 24, 2008 (edited) It's ironic you posted that, I was just coming back to reply again. I agree your first one is better, also, I couldn't get stringstripws (I didn't try too hard) to work with the second one, and it seems as though it is needed for the second one. I noticed the distinct difference in your version and Paulie's. His looks at the next word, and if the character count is going to exceed the max length, it puts it to the next line (which I like). With yours, if the character count is at 20 with a limit of 22 and the next word has 5 characters, it will allow that word and then let the next word start on the next line (EDIT: which I guess is how yours is able to take the punctuations into consideration); which is why I guess there was that initial confusion by me with your first example. Also Paulie's character count takes spaces and punctuations into consideration in its count. So I guess there's no real way to blend the two.?.?.Or am I wrong in what I'm seeing? Edited July 24, 2008 by Champak
Moderators SmOke_N Posted July 24, 2008 Moderators Posted July 24, 2008 It's ironic you posted that, I was just coming back to reply again. I agree your first one is better, also, I couldn't get stringstripws (I didn't try too hard) to work with the second one, and it seems as though it is needed for the second one. I noticed the distinct difference in your version and Paulie's. His looks at the next word, and if the character count is going to exceed the max length, it puts it to the next line (which I like). With yours, if the character count is at 20 with a limit of 22 and the next word has 5 characters, it will allow that word and then let the next word start on the next line; which is why I guess there was that initial confusion by me with your first example. Also Paulie's character count takes spaces and punctuations into consideration in its count. So I guess there's no real way to blend the two.?.?.Or am I wrong in what I'm seeing?Mine is one line of code, of course I could code exceptions... but then again... so can you ... As I personally don't have a need for it. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Champak Posted July 24, 2008 Author Posted July 24, 2008 Agreed, I'm actually trying to figure it out now.
Moderators SmOke_N Posted July 24, 2008 Moderators Posted July 24, 2008 I started getting lost in the code, so it looks a mess. I couldn't figure out how to do it straight RegEx because you have so many stipulations, so I improvised:expandcollapse popup$s_text = _ "THIS IS A REALLY LONG TEXT LINE THAT " & _ "WOULD MAKE A REALLY LONG MESSAGE BOX" & _ " AND SO WE WANT TO WORD WRAP IT BUT " & _ "NOT CUT OFF THE WORDS! IT IS 155 CHARS " & _ "LONG!!!!!!!!!!!!!!!!!!!!!!!!!!!. Another line." $s_output = _StringRegExpWordWrap2($s_text, 23) MsgBox(64, "Info", $s_output) Func _StringRegExpWordWrap2($s_string, $i_maxchars, $s_delim = @CRLF, $b_no_leading_spaces = True, $b_skip_cr_lf = True) Local $a_sre = StringRegExp($s_string, "(?s)(?:(\w+|[[:punct:]]+|(?:\r\n|\r|\n)+|\W+))", 3) Local $n_len, $n_ub = UBound($a_sre) - 1 Local $s_output, $s_return, $n_outlen, $i For $i = 0 To $n_ub $n_len = StringLen($a_sre[$i]) $n_outlen = StringLen($s_output) If ($n_outlen + $n_len > $i_maxchars) Then $s_return &= $s_output & $s_delim $s_output = "" EndIf If $b_skip_cr_lf And StringRegExp($a_sre[$i], "\r|\n") Then ContinueLoop If $b_no_leading_spaces And $s_output = "" And StringRegExp($a_sre[$i], "^\s+\z") Then ContinueLoop If $i < $n_ub Then If StringRegExp($a_sre[$i + 1], "^[[:punct:]]+\z") Then If (($n_outlen + $n_len + StringLen($a_sre[$i + 1])) > $i_maxchars) Then If (($n_len + StringLen($a_sre[$i + 1])) > $i_maxchars) Then If $s_output <> "" Then $s_return &= $s_output & $s_delim $s_return &= StringLeft($a_sre[$i] & $a_sre[$i + 1], $i_maxchars) & $s_delim $s_output = StringTrimLeft($a_sre[$i] & $a_sre[$i + 1], $i_maxchars) Else If $s_output <> "" Then $s_return &= $s_output & $s_delim $s_output = $a_sre[$i] & $a_sre[$i + 1] EndIf $i += 1 Else $s_output &= $a_sre[$i] EndIf Else $s_output &= $a_sre[$i] EndIf Else $s_output &= StringRegExpReplace($a_sre[$i], "(.{" & $i_maxchars & "})", "\1" & $s_delim) EndIf Next Return $s_return & $s_output EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now