how to replace A or B or C by "1" ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

how to replace A or B or C by "1" ?

17 Jul 2017, 02:42

I kow how te replace a single instance:

Code: Select all

Unwrapped := RegExReplace(Unwrapped, "\bA\b" , "1")
But how can I replace several instance in one?

(\b is to match the whole word)


(I did not find the answer here https://autohotkey.com/docs/commands/StringReplace.htm )
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: how to replace A or B or C by "1" ?

17 Jul 2017, 02:51

That code should replace several instances in one, because you omitted the Limit parameter in RegExReplace. (That is different from StringReplace.)

Even if you used StringReplace, so long as you omit the limit parameter in the function, it will replace all instances.

Edit, ooohh, you were looking to identify multiple possible needles in a single expression. My apologies, I misunderstood what you wanted. And that fault is on me because the code you provided should not support the hypothesis you were struggling with replacing multiple occurrences of the " A " instance.
Last edited by Exaskryz on 17 Jul 2017, 11:13, edited 1 time in total.
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: how to replace A or B or C by "1" ?

17 Jul 2017, 03:10

thank you for your reply but I don't understand it. Could you please give me an example of the syntax (replace A or B or C by "1")?
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: how to replace A or B or C by "1" ?

17 Jul 2017, 05:50

Thanks to Era_Scarecrow here is the answer:

Code: Select all

Unwrapped:=RegExReplace(Unwrapped, "(A|B|C)", "1")
or to search/replace for the word only:

Code: Select all

Unwrapped:=RegExReplace(Unwrapped, "(\bA\b|\bB\b|\bC\b)", "1")
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to replace A or B or C by "1" ?

17 Jul 2017, 10:03

Here are some ideas:

Code: Select all

q:: ;replace strings (whole-word match)
vText := "A B C AA BB CC A B C a b c aa bb cc a b c"
;case sensitive:
MsgBox, % RegExReplace(vText, "\b(A|B|C)\b", "1")
MsgBox, % RegExReplace(vText, "\b[ABC]\b", "1") ;if each needle is 1 char long

;case insensitive:
MsgBox, % RegExReplace(vText, "i)\b(A|B|C)\b", "1")
MsgBox, % RegExReplace(vText, "i)\b[ABC]\b", "1") ;if each needle is 1 char long
return
Btw 'single instance' sounds like you want to replace an item the first time it occurs, and ignore the other occurrences. Perhaps saying something like you want to replace multiple needles v. a single a needle would make more sense.

Era_Scarecrow, good name, is that from a live chat forum? Cheers.
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 (v1)”

Who is online

Users browsing this forum: No registered users and 294 guests