remove prefix from a phone number (string and variable inside strreplace) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
andy_a_713
Posts: 7
Joined: 12 Aug 2022, 02:07

remove prefix from a phone number (string and variable inside strreplace)

Post by andy_a_713 » 14 Aug 2022, 16:09

i'm trying to get the phone number without the prefix (+43 in this case but i want a general solution) and anything else but numbers but as soon as i put %prefix% instead of 43 in ReplaceTermsTel2 it stops working and outputs the phone number with it still attached. i tried having %prefix% inside the quotes and outside but nothing seems to work. it might be because the code can be written more efficiently with some other regex commands but i don't quite have the hang of it so if someone could help me out it would be greatly apreciated

Code: Select all

numpad6::
clipboard := "+43 664 / 333 2508"
sleep 100
prefix := 43
ReplaceTermsTel1 := {" " : "", "(" : "", ")" : "", "/" : "", "\" : ""}
ReplaceTermsTel2 := {"00"%prefix% : "","+"%prefix% : ""}
sleep 300
for c, d in ReplaceTermsTel1{
	Clipboard := StrReplace(Clipboard, c, d)
}
sleep 300
for i, j in ReplaceTermsTel2{
Clipboard := StrReplace(Clipboard, i, j)
}
sleep 300
Clipboard := RegExReplace(Clipboard, "\D", "")
msgbox, %Clipboard%

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

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by boiler » 14 Aug 2022, 16:14

Code: Select all

clipboard := "+43 664 / 333 2508"
MsgBox, % clipboard := RegExReplace(clipboard, "\+\d+\s+")

Assumes the prefix always starts with +. If that's not the case, describe the other possible cases.

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

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by boiler » 14 Aug 2022, 16:18

I guess you asked for all non-digits to be replaced also:

Code: Select all

clipboard := "+43 664 / 333 2508"
MsgBox, % clipboard := RegExReplace(RegExReplace(clipboard, "\+\d+"), "\D")

andy_a_713
Posts: 7
Joined: 12 Aug 2022, 02:07

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by andy_a_713 » 14 Aug 2022, 16:21

boiler wrote:
14 Aug 2022, 16:14

Code: Select all

clipboard := "+43 664 / 333 2508"
MsgBox, % RegExReplace(clipboard, "\+\d+\s+")

Assumes the prefix always starts with +. If that's not the case, describe the other possible cases.
the other cases are when the number is something like "00prefix" or "+ prefix" or "+(prefix)"
and im sorry to ask this but for the life of me i dont understand how regex replace works, could you please explain what that

Code: Select all

 MsgBox, % RegExReplace(clipboard, "\+\d+\s+")
does

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

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by boiler » 14 Aug 2022, 16:27

Code: Select all

clipboard := "+(43) 664 / 333 2508"
MsgBox, % clipboard := RegExReplace(RegExReplace(clipboard, "^(\+\d+|00\d+|(\+\(\d+)\))"), "\D")
Plug in the patern at https://regex101.com/ and hover over the different tokens to get an explanation of what they are.

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

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by boiler » 14 Aug 2022, 16:30

Use the version above that shows up now because I edited it to remove an unnecessary character.

andy_a_713
Posts: 7
Joined: 12 Aug 2022, 02:07

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by andy_a_713 » 14 Aug 2022, 16:30

thank you so much boiler, for answering me and for the other responses you've given to other questions i had in the past. you're a life-saver

andy_a_713
Posts: 7
Joined: 12 Aug 2022, 02:07

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by andy_a_713 » 14 Aug 2022, 17:05

boiler wrote:
14 Aug 2022, 16:27

Code: Select all

clipboard := "+(43) 664 / 333 2508"
MsgBox, % clipboard := RegExReplace(RegExReplace(clipboard, "^(\+\d+|00\d+|(\+\(\d+)\))"), "\D")
Plug in the patern at https://regex101.com/ and hover over the different tokens to get an explanation of what they are.
im so sorry but i just realized that numbers can also be in this form : "+prefix015336888" or (+prefix)015336888
can variables be introduced in regexreplace? if so how?

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

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by boiler » 14 Aug 2022, 18:18

This is how most RegEx threads go. After an initial description, then it keeps getting tweaked with more cases. I'm not going to go down the path of adding the new cases, but hopefully you can figure it out and modify it as needed.

Variables can be introduced into RegEx patterns just like any other expression. Example:

Code: Select all

Name := "Bob"
MsgBox, "Please tell " . Name . " that we're ready for him now."

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by sofista » 14 Aug 2022, 19:11

Agree with boiler about the endless changes in regular expression posts. However, I think in this case there might be a simpler and more definitive solution (ha ha, never give up hope).

@andy_a_713 From reading your initial code, it seems that all you wanted is the phone number, always preceded by a two-digit prefix - and a possible non-numeric character structure -, which should be removed. If this is true, then the problem could be solved as follows:

Code: Select all

Clipboard := "+(43) 664 / 333 2508"
MsgBox, % clipboard := SubStr(RegExReplace(clipboard, "\D"), 3)    ; 6643332508
Clipboard := "+43015336888"
MsgBox, % clipboard := SubStr(RegExReplace(clipboard, "\D"), 3)    ; 015336888
Clipboard := "(+43)015336888"
MsgBox, % clipboard := SubStr(RegExReplace(clipboard, "\D"), 3)    ; 015336888

andy_a_713
Posts: 7
Joined: 12 Aug 2022, 02:07

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by andy_a_713 » 14 Aug 2022, 19:30

Thanks again for all the help.
@sofista your idea would work except that my code searches a phone number on a website of my choosing and
sometimes it has no prefix and somethimes it starts with 00%prefix%. And the prefix is determined by the country that was selected in a dropdownlist and some countries have a 3 nr prefix and im trying to find a solutions that works no matter the prefix or the formating of the website developer. But i think i got an idea how to solve it tomorrow since now ik how to add variables in regex.

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by sofista » 14 Aug 2022, 20:05

Thank you for considering it. Also, please note that it would have been more appropriate for this information to be given at the beginning of the thread. As they say, help us to help you.

Happy regex

andy_a_713
Posts: 7
Joined: 12 Aug 2022, 02:07

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by andy_a_713 » 15 Aug 2022, 08:16

ok so i ended up with this, which should cover everything, whether the number has other characters between the prefix and itself or not, whether there's a other characters between the plus and the prefix or not, and it should work even if the number is written without the prefix but starts with whatever the prefix is, and sometimes numbers have a "(0)" or a "(1)" inside of them which needs to be removed as well. my only problem now is that as soon as i replace the "43" in the last part of the regex with a variable (prefix) it doesn't work anymore and i don't get why(i tried putting the variable in between % but it still doesn't work)

Code: Select all

prefix := 43
MsgBox, % clipboard := RegExReplace(RegExReplace(RegExReplace(clipboard, "(\(0\))|(\(1\))"), "\\|\/|\)|\(|\s|\."), "^(\+. prefix .|00. prefix .)")

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by sofista » 15 Aug 2022, 11:40

Haven't tested the RegEx, but the syntax of the last RegExReplace is incorrect:

Code: Select all

"^(\+. prefix .|00. prefix .)"
variables should be unquoted, as boiler's example showed before:

Code: Select all

"^(\+." prefix ".|00." prefix ".)"

andy_a_713
Posts: 7
Joined: 12 Aug 2022, 02:07

Re: remove prefix from a phone number (string and variable inside strreplace)  Topic is solved

Post by andy_a_713 » 15 Aug 2022, 16:56

yep this did it. i just removed the periods cuz they wern't needed. thanks again. its a bit long and twisted but it works and its better than what i had before

Code: Select all

prefix := 43
MsgBox, % clipboard := RegExReplace(RegExReplace(RegExReplace(RegExReplace(clipboard, "(\(0\))|(\(1\))"), "\\|\/|\)|\(|\s|\."), "^(\+" prefix "|00" prefix ")"), "\D")

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: remove prefix from a phone number (string and variable inside strreplace)

Post by sofista » 15 Aug 2022, 20:10

You are welcome. Let me suggest you to read in the docs how to deal with the expression syntax.

Post Reply

Return to “Ask for Help (v1)”