How to invert Regex match operator when using it in ternary operators? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gary-Atlan82
Posts: 74
Joined: 07 Mar 2023, 05:20

How to invert Regex match operator when using it in ternary operators?

Post by Gary-Atlan82 » 24 Mar 2023, 08:42

For example, the below, I want to check if the initial Thiswindow variable contains AHK_ID or A, if it does NOT then assign 111 else assign 000

The below, always assigns 111 to ThisWindow, I would like it to NOT in this case, cant the operator ~= not be inverted, like so !~= or even ~!=?

Code: Select all

ThisWindow := "Ahk_ID 0x12345"
ThisWindow := ThisWindow ~!= "Ahk_ID 0x" || ThisWindow != "A" ? "111"  : "000"
OutputDebug, %ThisWindow%
Any help would be greatly appreciated!

cat dog fox war
Posts: 38
Joined: 15 Mar 2023, 10:18

Re: How to invert Regex match operator when using it in ternary operators?  Topic is solved

Post by cat dog fox war » 24 Mar 2023, 10:47

Code: Select all

ThisWindow := "Ahk_ID 0x12345"
ThisWindow := !(ThisWindow ~= "Ahk_ID 0x") || ThisWindow != "A" 
    ? "111"
    : "000"
OutputDebug, %ThisWindow%
Just wrap parentheses and use ! to reverse true and false.
the !~= or ~!= operator ,I can't find it in the documentation , https://www.autohotkey.com/docs/v1/Variables.htm#operators

Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to invert Regex match operator when using it in ternary operators?

Post by Rohwedder » 25 Mar 2023, 06:15

Hallo,
try:

Code: Select all

ThisWindow := "Ahk_ID 0x12345"
ThisWindow := ThisWindow ~= "Ahk_ID|A" ? "000" : "111"
OutputDebug, %ThisWindow%
"Ahk_ID|A" means "Ahk_ID" or "A"
(or simpler: "A")

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to invert Regex match operator when using it in ternary operators?

Post by swagfag » 25 Mar 2023, 07:22

but its not equivalent to what he wrote originally. it would have to be

Code: Select all

ThisWindow ~= "Ahk_ID 0x|^(?i:A)$" ? "000" : "111"

Gary-Atlan82
Posts: 74
Joined: 07 Mar 2023, 05:20

Re: How to invert Regex match operator when using it in ternary operators?

Post by Gary-Atlan82 » 26 Mar 2023, 15:21

Hey I have it working now, thank you all

Post Reply

Return to “Ask for Help (v1)”