 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
|
| Back to top |
|
 |
ghee22
Joined: 04 Jul 2009 Posts: 36
|
Posted: Sat Nov 07, 2009 6:22 pm Post subject: |
|
|
| Titan wrote: | Following a recent discussion I remembered this thread and how regular expressions can be used to simplify most things. Here's a new version with comments: | Code: | StringMod(string, method, param1 = "", param2 = "") {
If method = Only ; remove all characters not in param1 from string:
new := RegExReplace(string, "i)[^" . param1 . "]")
Else If method = Omit ; remove characters in param1 from string:
new := RegExReplace(string, "i)[" . param1 . "]")
Else If method = Flip ; flip string backwards:
Loop, % length := StrLen(string) ; loop for the length of the string
new .= SubStr(string, length - A_Index, 1) ; get character from the end going backwards
Else If method = Replace ; replace param1 with param2
new := RegExReplace(string, "i)" . param1, param2)
Else If RegExMatch(method, "Rot(?:ate)?(\d+)", rotN) ; for Rot13 and Rot47:
{
Loop, Parse, string ; for each character in the string:
new .= Chr((chr := Asc(A_LoopField)) + rotN1) - 94 * (chr > 126)) ; apply rotation transformation
; this increases the ASCII value of the character by the specified rotation amount
; if the value exceeds 126 (highest character) subtract by 94 (character range)
}
Else If method = Scramble ; rearrange characters in random order
{
new := RegExReplace(string, "(.)", "$1÷") ; delimit each character with '÷'
; this high-ASCII character is unlikely to exist in string, if it does it will be removed
Sort, new, Random Z D÷ ; randomly sort
new := RegExReplace(new, "÷") ; remove delimiter
}
Else If method = Bloat ; pad each character with param1 and param2:
new := RegExReplace(string, "(.)", param1 . "$1" . param2)
Else If method = Pattern ; return the number of times each character appears in the string:
{
unique := RegExReplace(string, "(.)", "$1÷") ; delimit each character with '÷' (like in 'Scramble')
Sort, unique, U Z D÷ ; remove duplicates
unique := RegExReplace(unique, "÷") ; remove delimiter
Loop, Parse, unique ; for each unique character in the string:
{
StringReplace, string, string, %A_LoopField%, , UseErrorLevel ; get the number of replacements
new .= A_LoopField . ErrorLevel ; store this alongside the character itself
}
}
Return, new ; return the modified string
} | Untested but I'm sure it works. |
Hello Titan, the latest AHK version (1.0.48.05) gives this error upon compiling:
| Code: | StringMod.ahk (14) : ==> Missing "("
Specifically: )
>Exit code: 2 Time: 0.221 |
Anyone have a fix? |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Sat Nov 07, 2009 7:08 pm Post subject: |
|
|
| ghee22 wrote: | | Code: | | StringMod.ahk (14) : ==> Missing "(" |
|
Line 14 is:
| Code: | | new .= Chr((chr := Asc(A_LoopField)) + rotN1) - 94 * (chr > 126)) |
| Quote: | | new .= Chr((...(...))...)...(...)) |
_________________ "Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried."
Antonio França
My stuff: Google Profile |
|
| 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
|