how to stringsplitt this Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
peter_ahk
Posts: 164
Joined: 13 Feb 2024, 14:49

how to stringsplitt this

Post by peter_ahk » 24 May 2024, 14:28

hello folks i would like some help

i struggle to understand the steps needed to splitt this into just the 6 behind CW and the 6 behind CT as separate values, in a way that also works when the string has these 2 things at the start for example or in the middle

Code: Select all

fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1

rockitdontstopit
Posts: 107
Joined: 12 Nov 2022, 15:47

Re: how to stringsplitt this

Post by rockitdontstopit » 24 May 2024, 14:45

peter_ahk wrote:
24 May 2024, 14:28
i struggle to understand the steps needed to splitt this into just the 6 behind CW and the 6 behind CT as separate values, in a way that also works when the string has these 2 things at the start for example or in the middle
This works for all 3 scenarios. I just tested it.

Code: Select all

string := "fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1"
; Extract the 6 characters following "CW"
RegExMatch(string, "CW([0-9A-F]{6})", cwMatch)
cwDigits := cwMatch1

; Extract the 6 characters following "CT"
RegExMatch(string, "CT([0-9A-F]{6})", ctMatch)
ctDigits := ctMatch1

if (cwDigits = "")
    MsgBox, CW digits not found.
else
    MsgBox, The digits after CW are: %cwDigits%

if (ctDigits = "")
    MsgBox, CT digits not found.
else
    MsgBox, The digits after CT are: %ctDigits%

peter_ahk
Posts: 164
Joined: 13 Feb 2024, 14:49

Re: how to stringsplitt this

Post by peter_ahk » 24 May 2024, 14:54

rockitdontstopit wrote:
24 May 2024, 14:45
This works for all 3 scenarios. I just tested it.

Code: Select all

string := "fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1"
; Extract the 6 characters following "CW"
RegExMatch(string, "CW([0-9A-F]{6})", cwMatch)
cwDigits := cwMatch1

; Extract the 6 characters following "CT"
RegExMatch(string, "CT([0-9A-F]{6})", ctMatch)
ctDigits := ctMatch1

if (cwDigits = "")
    MsgBox, CW digits not found.
else
    MsgBox, The digits after CW are: %cwDigits%

if (ctDigits = "")
    MsgBox, CT digits not found.
else
    MsgBox, The digits after CT are: %ctDigits%
oh wauw thats awesome i was looking at the wrong commands (not that i would have made sense of this) ill have time to test later tonight hope to leave the green checkmark later today

User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: how to stringsplitt this  Topic is solved

Post by JoeWinograd » 24 May 2024, 15:13

Hi Peter,

If you're not a RegEx fan, this can be done easily with standard string operations. This assumes that there is one one-and-only-one occurrence of CT, and one-and-only-one occurrence of CW (both case insensitive, but you can make it case sensitive by adding 1 as the third parameter to the InStr call):

Code: Select all

TestString:="fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1"
CT6:=StringSplitThis(TestString,"CT")
CW6:=StringSplitThis(TestString,"CW")
MsgBox,4160,String Split Test,CT6=%CT6%`nCW6=%CW6%
ExitApp

StringSplitThis(Haystack,ID)
{
  Return SubStr(Haystack,InStr(Haystack,ID)+2,6)
}
Note that the function works for any unique two-character ID (not just CT and CW) and for the ID to be in any position in the string. Regards, Joe

peter_ahk
Posts: 164
Joined: 13 Feb 2024, 14:49

Re: how to stringsplitt this

Post by peter_ahk » 24 May 2024, 15:23

JoeWinograd wrote:
24 May 2024, 15:13
Hi Peter,

If you're not a RegEx fan, this can be done easily with standard string operations. This assumes that there is one one-and-only-one occurrence of CT, and one-and-only-one occurrence of CW (both case insensitive, but you can make it case sensitive by adding 1 as the third parameter to the InStr call):

Code: Select all

TestString:="fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1"
CT6:=StringSplitThis(TestString,"CT")
CW6:=StringSplitThis(TestString,"CW")
MsgBox,4160,String Split Test,CT6=%CT6%`nCW6=%CW6%
ExitApp

StringSplitThis(Haystack,ID)
{
  Return SubStr(Haystack,InStr(Haystack,ID)+2,6)
}
Note that the function works for any unique two-character ID (not just CT and CW) and for the ID to be in any position in the string. Regards, Joe
very interesting thank you joe i had not considered case sensitive and since it is a user adjustable string bolth ways could be needed and i think this is the better option then for my use case

peter_ahk
Posts: 164
Joined: 13 Feb 2024, 14:49

Re: how to stringsplitt this

Post by peter_ahk » 25 May 2024, 08:51

ended up using the second option for it not being case sensitive, bolth options where what i asked tho and bolt showed me things i did not know so good learning here thank you

User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: how to stringsplitt this

Post by JoeWinograd » 25 May 2024, 11:41

peter_ahk wrote:ended up using the second option for it not being case sensitive
Hi Peter,
You can make the RegEx case insensitive by adding the i) option to the needle. For example, in the code that @rockitdontstopit posted, change the RegExMatch lines to these:

Code: Select all

RegExMatch(string, "i)CW([0-9A-F]{6})", cwMatch)
RegExMatch(string, "i)CT([0-9A-F]{6})", ctMatch)
Regards, Joe

peter_ahk
Posts: 164
Joined: 13 Feb 2024, 14:49

Re: how to stringsplitt this

Post by peter_ahk » 25 May 2024, 11:48

JoeWinograd wrote:
25 May 2024, 11:41
peter_ahk wrote:ended up using the second option for it not being case sensitive
Hi Peter,
You can make the RegEx case insensitive by adding the i) option to the needle. For example, in the code that @rockitdontstopit posted, change the RegExMatch lines to these:

Code: Select all

RegExMatch(string, "i)CW([0-9A-F]{6})", cwMatch)
RegExMatch(string, "i)CT([0-9A-F]{6})", ctMatch)
Regards, Joe
that is good to know thank you joe this thread is going into my solutions folder :)

User avatar
Chunjee
Posts: 1500
Joined: 18 Apr 2014, 19:05
Contact:

Re: how to stringsplitt this

Post by Chunjee » 26 May 2024, 12:46

peter_ahk wrote:
24 May 2024, 14:28
i struggle to understand the steps needed to splitt this into just the 6 behind CW and the 6 behind CT as separate values, in a way that also works when the string has these 2 things at the start for example or in the middle

Code: Select all

fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1


my personal inclination would be to filter these considering fs14 ZY7 w190 h40 x865 y520 are all thrown out. Sorry I didn't understand the "or in the middle" instruction as no example of that was given.

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk


TestString := "fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1"
myData := A.filter(strSplit(TestString, " "), func("filterCWandCT"))
filterCWandCT(inputVar) {
	if (biga.startsWith(inputVar, "CW") || biga.startsWith(inputVar, "CT")) {
		return true
	}
}
; => ["CW50595E", "CTE1E1E1"]
https://biga-ahk.github.io/biga.ahk/#/?id=filter

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: how to stringsplitt this

Post by boiler » 26 May 2024, 14:10

Here’s a ReEx version that doesn’t use a subpattern, so the match variable is your final variable name of choice (does not use a 1 after it to identify the subpattern):

Code: Select all

string := "fs14 ZY7 w190 h40 x865 y520 CW50595E CTE1E1E1"
RegExMatch(string, "CW\K[0-9A-F]{6}", CW)
RegExMatch(string, "CT\K[0-9A-F]{6}", CT)
Msgbox, % "CW:`t" CW "`nCT:`t" CT

peter_ahk
Posts: 164
Joined: 13 Feb 2024, 14:49

Re: how to stringsplitt this

Post by peter_ahk » 26 May 2024, 22:39

thnx folks lots of solutions here for different situations :thumbup:

Post Reply

Return to “Ask for Help (v1)”