Problem with RegExReplace Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SyntaxTerror
Posts: 45
Joined: 23 May 2017, 12:55

Problem with RegExReplace

Post by SyntaxTerror » 18 Oct 2021, 20:00

Hello

I am trying to make a script that removes letters from a variable containing all the letters of the alphabet.

Here is my (faulty) script:

Code: Select all

Alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ
Loop
{
	InputBox, Word , , Remaining letters :`n%Alphabet%, , 300, 150, , , Locale
	if ErrorLevel
	ExitApp
	Else
	Alphabet := RegExReplace(Alphabet, "i)[%Word%]*", "")
}
This regex should work (test here : https://regex101.com/r/42OAC8/2), but I don't know what is going wrong here.

It may be just a problem with the variables syntax, I'm not very good at this...

Thank you.

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: Problem with RegExReplace  Topic is solved

Post by mikeyww » 18 Oct 2021, 20:12

Functions accept expressions.

Code: Select all

Alphabet := RegExReplace(Alphabet, "i)[" Word "]")
Explained: Expressions

User avatar
SyntaxTerror
Posts: 45
Joined: 23 May 2017, 12:55

Re: Problem with RegExReplace

Post by SyntaxTerror » 18 Oct 2021, 20:34

Thank you very much for this quick answer, @mikeyww .

Another thing: if I want to do this for different sets of letters, I can add another loop with Alphabet = ABCD... inside, and replace the if ErrorLevel ExitApp by if ErrorLevel Break, but then I cannot exit the script/App.

Clicking the red X on the top right of the window seem to do the same as "Cancel", and there does not seem to be an option in InputBox for a third button...

Is there is a way to do this without having to use the GUI window (I'm not very confortable at designing this GUI thingy)?

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: Problem with RegExReplace

Post by mikeyww » 18 Oct 2021, 20:43

What do you mean by different sets of letters? Explain in detail, step by step. Include an example.

User avatar
SyntaxTerror
Posts: 45
Joined: 23 May 2017, 12:55

Re: Problem with RegExReplace

Post by SyntaxTerror » 18 Oct 2021, 20:49

If I have this code:

Code: Select all

Loop
{
	Alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Loop
	{
		InputBox, Word , , Remaining letters :`n%Alphabet%, , 300, 150, , , Locale
		if ErrorLevel
		Break
		Else
		Alphabet := RegExReplace(Alphabet, "i)[" Word "]")
	}
}
If I press "Cancel" or the top-right red X, I'll only reset the variable Alphabet to its original value.

But how can I exit the programme entirely? (without having to use the GUI window if possible)

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: Problem with RegExReplace

Post by mikeyww » 18 Oct 2021, 21:25

Code: Select all

ask("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), ask("12345")

ask(chars) {
 Loop {
  InputBox, word,, Remaining characters:`n%chars%,, 300, 150
  If (ErrorLevel || word = "")
   Return
  chars := RegExReplace(chars, "i)[" word "]")
 }
}

User avatar
SyntaxTerror
Posts: 45
Joined: 23 May 2017, 12:55

Re: Problem with RegExReplace

Post by SyntaxTerror » 18 Oct 2021, 21:43

Thank you @mikeyww

I don't really understand your code, but it seems to work.

I'll have to study it more closely if I want to be able to do the same thing in the future!

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: Problem with RegExReplace

Post by mikeyww » 18 Oct 2021, 21:46

It's about the same as your script, but moves it into a function ("ask"), and then calls that function twice. The character sequence is passed as a parameter to the function.

Post Reply

Return to “Ask for Help (v1)”