 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
dijiyd
Joined: 01 Apr 2004 Posts: 90 Location: Philippines
|
Posted: Fri Feb 25, 2005 2:43 pm Post subject: StringReplace, onceButNotTwoOrMoreOccurencesOfThis |
|
|
Um... is there something like this out there? For example, `newlines. I want to take out all single newlines, but not multiple newlines. (Yeah, I know all about regexes and how it's not available yet.) Is there any workaround for this in AHK?
[Yeah, I'm making something that formats plain text to put on the ebook reader on my mobile. The screen is considerably smaller of course, so the manual linefeeds that appears on nearly all Ebooks for comfortable 72-character-line makes it uncomfortable. (imagine every one and a half lines there is a linefeed. My head hurts.) But I also want to preserve the 2 or more newlines for paragraph division purposes.] |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3842 Location: Bremen, Germany
|
Posted: Fri Feb 25, 2005 2:56 pm Post subject: |
|
|
I suppose, that you mean by two newlines, that the second line is a empty line, right?
So you could loop through the text and concate all lines to be one, if a empty line shows up, add a newline. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
dijiyd
Joined: 01 Apr 2004 Posts: 90 Location: Philippines
|
Posted: Fri Feb 25, 2005 4:00 pm Post subject: |
|
|
Yeah... I tried it, but there's something wrong:
| Code: | Ebook =
Send, ^c
ClipWait
Loop, Parse, Clipboard, `r`n
{
; msgbox, %A_LoopField% --> debug
if A_LoopField =
{
Ebook = %ebook%`n
}
else
{
Ebook = %ebook%%A_LoopField%
}
}
clipboard = %ebook%
return
|
It kinda doubles the lines. I tried msgbox'ing the A_LoopFields, and it sees double the newlines. I dunno. I tried using Loop... `n, but this one gave me just a reaaaaly long line, without newlines (but it A_LoopField displays the lines. It's the output that had no newlines.). Any help? |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Fri Feb 25, 2005 4:44 pm Post subject: |
|
|
| Code: | Clipboard = A line`nAnother line`n`n`nA last line
MsgBox, %Clipboard%
Loop, Parse, Clipboard, `n, `r
{
; msgbox, %A_LoopField% --> debug
if A_LoopField =
{
NLCount ++ ; found empty line, count it and concat it to a prev`. found line
If NLCount = 2 ; consecutive empty line found so ignore it
{
NLCount = 0
Continue
}
}
Ebook = %ebook%%A_LoopField%`n
}
MsgBox, %ebook% |
Tested.
I'm not clear if I've missed you about the task to accomplish ...
Might be useless. |
|
| Back to top |
|
 |
dijiyd
Joined: 01 Apr 2004 Posts: 90 Location: Philippines
|
Posted: Fri Feb 25, 2005 4:51 pm Post subject: |
|
|
lol... fixed it. It looks like "var = " does not mean that the var is empty. Here's the new and imroved script, if anyone wants it.
| Code: |
Ebook =
EbookPrevLineIsSpace = 0
Send, ^c
ClipWait
StringReplace, clipboard, clipboard, %A_Space%%A_Space%, , A
ClipWait
Loop, Parse, Clipboard, `n
{
if A_LoopField is space
{
EBookPrevLineIsSpace := EBookPrevLineIsSpace + 1
}
else
{
if EBookPrevLineIsSpace > 1
{
Ebook =%ebook%`r`n`r`n`r`n%A_LoopField%
}
else if EBookPrevLineIsSpace = 1
{
Ebook =%ebook%`r`n`r`n%A_LoopField%
}
else if EbookPrevLineIsSpace = 0
{
Ebook =%ebook% %A_LoopField%
}
EbookPrevLineIsSpace := 0
}
}
clipboard =%ebook%
return
|
It now replaces ALL newlines occuring more than twice to 3 newlines, for clean sectioning purposes, newlines occuring twice to well, twice, and removes newlines occuring once. Also, it removes indents-- well, actually, it looks very specific for my purposes so it's probably rare that anyone would want it.
Edit:
Uh... yeah, sorry BoBo, I was working on it already. I just couldn't post it in time. Yeah, works great too. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|