RegEx: add/remove characters periodically

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

RegEx: add/remove characters periodically

04 Dec 2018, 11:27

- I was experimenting with adding/removing characters periodically via RegEx. I thought I'd share some of the code.
- Some of the code can be quite fiddly, so do notify of any issues.
- Other people are welcome to post/link to other examples related to periodic character addition/removal.

Code: Select all

q:: ;add a zero before each character, then remove the first character in each pair
vNum := "10001011"
Loop, 8
{
	MsgBox, % RegExReplace(vNum, ".{8}", "$0 ")
	vNum := RegExReplace(vNum, ".", "0$0")
}
Loop, 9
{
	MsgBox, % RegExReplace(vNum, ".{8}", "$0 ")
	vNum := RegExReplace(vNum, ".(.)", "$1")
}
return

;==================================================

w:: ;add periodic characters
;4-character groups: add to end
vText := JEE_StrRept("abcd", 10) "ABC"
MsgBox, % RegExReplace(vText, ".{4}", "$0_")
vText := JEE_StrRept("abcd", 10) "ABCD"
MsgBox, % RegExReplace(vText, ".{4}", "$0_")

;4-character groups: add to start, middle and end
;note: be aware of any incomplete character groups at the end of a string
vText := JEE_StrRept("abcd", 10) "ABC"
MsgBox, % RegExReplace(vText, "(..)(..)", "<$1_$2>")
vText := JEE_StrRept("abcd", 10) "ABCD"
MsgBox, % RegExReplace(vText, "(..)(..)", "<$1_$2>")
return

;==================================================

e:: ;remove periodic characters (simple approach but with a caveat)
;note: caveat: if dealing with an n-character cycle, the string length must be a multiple of n
;note: trailing capital letters to test handling of end characters

;2-character groups
vText := JEE_StrRept("ab", 10) "A"
MsgBox, % RegExReplace(vText, "(.).", "$1")
MsgBox, % RegExReplace(vText, ".(.)", "$1")

;3-character groups
vText := JEE_StrRept("abc", 10) "AB"
MsgBox, % RegExReplace(vText, "(.)..", "$1")
MsgBox, % RegExReplace(vText, ".(.).", "$1")
MsgBox, % RegExReplace(vText, "..(.)", "$1")
MsgBox, % RegExReplace(vText, "(..).", "$1")
MsgBox, % RegExReplace(vText, ".(..)", "$1")
MsgBox, % RegExReplace(vText, "(.).(.)", "$1$2")
return

;==================================================

r:: ;remove periodic characters
;note: trailing capital letters to test handling of end characters

;2-character groups
vText := JEE_StrRept("ab", 10) "A"
MsgBox, % RegExReplace(vText, "(.).?", "$1")
MsgBox, % RegExReplace(vText, ".(.)?", "$1")

;3-character groups
vText := JEE_StrRept("abc", 10) "AB"
MsgBox, % RegExReplace(vText, "(.).?.?", "$1")
MsgBox, % RegExReplace(vText, ".(.)?.?", "$1")
MsgBox, % RegExReplace(vText, "..?(.)?", "$1")
MsgBox, % RegExReplace(vText, "(.)(.)?.?", "$1$2")
MsgBox, % RegExReplace(vText, ".(.)?(.)?", "$1$2")
MsgBox, % RegExReplace(vText, "(.).?(.)?", "$1$2")
return

;==================================================

JEE_StrRept(vText, vNum)
{
	local
	if (vNum <= 0)
		return
	return StrReplace(Format("{:" vNum "}", ""), " ", vText)
	;return StrReplace(Format("{:0" vNum "}", 0), 0, vText)
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: BushMange, CEA6597, VACO BenQ, w0z and 187 guests