Phone numbers or text in a variable Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Phone numbers or text in a variable

13 Mar 2020, 16:23

Hi!
I have a variable that can contain a phone number (or letters / words) - a maximum of 11 characters.
1) I want to know if the variable have a phone number (in the local format eg 0721234567)
  • No spaces
  • Only numbers
  • Always starts with zero
  • Must always contain 8-10 digits
If the contents of the variable meet these criteria for a telephone number, I want to create the following structure (0046701234567)

For example
- this is phone numbers
  • Input = 0701234567
  • Input = 070 123 45 67
- and this is NOT phone numbers
  • Input = 1234567
  • Input = 07312349876
  • Input = 7012345678
  • Input = 070-1234567
  • Input = Kalle
Maybe an
Started, but got stuck in how I can know that there are only numbers in the variable.
User avatar
boiler
Posts: 16916
Joined: 21 Dec 2014, 02:44

Re: Phone numbers or text in a variable

13 Mar 2020, 20:07

This works as intended with all the examples:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

; Phone numbers (8-10 numbers)
Input = 0701234567
; Input = 070 123 45 67


; Not Phone numbers
; Input = 1234567
; Input = 07312349876
; Input = 7012345678
; Input = 070-1234567
; Input = Kalle

Input := StrReplace(Input, A_Space,, all)
if RegExMatch(Input, "^\D*(0\d{7,9})\D*$", Match)
	PhoneNo := SubStr("00000", 1, 13 - StrLen(Match1)) . Match1
else
{	MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, % "No Phonenumber .: " Input
	ExitApp
}

; Result .: 0046701234567
MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, % "Yes! - OK! `n`nPhonenumber .: ->" PhoneNo "<-"
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Phone numbers or text in a variable

14 Mar 2020, 03:55

boiler wrote:
13 Mar 2020, 20:07
This works as intended with all the examples:...
(Nice solution) Checking if it's a phone number or not, seem to work perfectly.
But, I wish for a slightly different result for approved phone number.
Not .: 0000701..., but instead 0046701...

Could you explain of the RegExMatch and ) . Match1 for me? :?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Phone numbers or text in a variable

14 Mar 2020, 04:07

Replace the boiler RegexMatch with this:

Code: Select all

if RegExMatch(Input, "^\D*0(\d{7,9})\D*$", Match)
	PhoneNo := "0046" . Match1
RegexMatch docs says
OutputVar

Mode 1 (default): Specify a variable in which to store the part of Haystack that matched the entire pattern. If the pattern is not found (that is, if the function returns 0), this variable and all array elements below are made blank.

If any capturing subpatterns are present inside NeedleRegEx, their matches are stored in a pseudo-array whose base name is OutputVar. For example, if the variable's name is Match, the substring that matches the first subpattern would be stored in Match1, the second would be stored in Match2, and so on.
Last edited by Odlanir on 14 Mar 2020, 05:34, edited 1 time in total.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Phone numbers or text in a variable

14 Mar 2020, 04:21

Nop! (it doesn't work)
Input = 0701234567 - the result should be .: 0046701234567... (not 004070...)

Next problem I found is .: (excuse the bad description of my wish)
If the phone number is shorter (eg 8 digits 07012345) the result should be 00467012345 (11 digits) - always change the first 0 to 0046
The next digit (after the first 0) should always be 1-9 (i.e. the result can never be 00460) eg. the original number Input = 0070123456 is not a phone number.
Did it get much more complicated?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Phone numbers or text in a variable

14 Mar 2020, 04:45

Albireo wrote:
14 Mar 2020, 04:21
Nop! (it doesn't work)
Input = 0701234567 - the result should be .: 0046701234567... (not 004070...)
....
Sure?
Using my regex the result with your input IS 0046701234567 .
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
boiler
Posts: 16916
Joined: 21 Dec 2014, 02:44

Re: Phone numbers or text in a variable

14 Mar 2020, 09:08

Albireo wrote: But, I wish for a slightly different result for approved phone number.
Not .: 0000701..., but instead 0046701...
I think we need a little more information to make sure this will be handled right for all cases. Will the leading 0 always be stripped before adding 0046 to the front of it? You said the number could be between 8 and 10 digits. Do you need the final number with the 0046 added to be a certain length, or will it vary in length depending on the length of the base number? It would be best if you could show several cases of before and after.
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Phone numbers or text in a variable

14 Mar 2020, 11:27

Thanks for your time!
boiler wrote:
14 Mar 2020, 09:08
... Will the leading 0 always be stripped before adding 0046 to the front of it?
Yes!
boiler wrote:
14 Mar 2020, 09:08
..the number could be between 8 and 10 digits.
Yes! (But I'm changing it now)
Right now it feels better to focus on the following. Only phone numbers containing 10 digits can be used. eg. 0701234567
(although in exceptional cases there are both shorter and longer mobile numbers)
It is better to check that all mobile numbers is starting with the following sequences.: (in Sweden) All mobile numbers start with the following numbers 070, 072, 073, 076, or 079 In this case, it is probably best to start from a list so that if new / other digit combinations emerge, it will be easy to add to the AHK-program.
boiler wrote:
14 Mar 2020, 09:08
Do you need the final number with the 0046 added to be a certain length, or will it vary in length depending on the length of the base number?
Since I skipped phone numbers other than 10 digits, the result will always be .: 10 digits minus the initial 0 plus 0046 = 13 digits total.

For Example
These phone numbers have a correct number structure
  • Input = 0701234567 Result .: 0046701234567
  • Input = 070 123 45 67 Result .: 0046701234567
  • Input = 0721234567 Result .: 0046721234567
- and this is NOT phone numbers
  • Input = 1234567 (too few digits and does not start with 0)
  • Input = 0741234567 (Starts with 074 - not with the above combinations)
  • Input = 7012345678 (Does not start with 0)
  • Input = 070-1234567 (hyphen is not a number)
  • Input = Kalle (Not a number :roll:)
If it is not a valid mobile number, it may contain a maximum of 11 characters / words / numbers. But I'll handle with that later.

Thanks in advance
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: Phone numbers or text in a variable  Topic is solved

15 Mar 2020, 07:15

This program seems to meet my goal of my desire, and is written to facilitate test running.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

StartPh := "070,072,073,076,079"

Var = 
( LTrim Comments
	; Phone numbers (8-10 numbers)
	0766544321
	0701234567
	070 321 45 67
	079 22 33 44 1
	
	; NO phone numbers
	073 AbcDefg
	0741234567
	1234567
	07312349876
	7012345678
	070-1234567
	Kalle
)
Var := StrSplit(Var,"`n")


Loop % Var.Length()
{	Input := Var[A_Index]	; Convert the array to a variable

	; Initiate
	PhoneNumber := False
	InputMod := StrReplace(Input, A_Space,, all)	; Remove spaces
	
	; Check if the beginning of the number is in the list
	CheckNo := SubStr(InputMod, 1, 3)
	If CheckNo in %StartPh%
	{	; Check if the number has 10 digits and a beginning from the list
		If (StrLen(InputMod) = 10)
		{	; Check if the number is only digits
			If InputMod is digit 
				PhoneNumber := True
	;		else	; NOT only digits
	;			PhoneNumber := False
		}
	;	else	; NOT 10 digits
	;		PhoneNumber := False
	}
	; else 	; NOT in the list
	;	PhoneUmber := False
	
	If PhoneNumber
		MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % A_Index "`nPhone number OK! .: " InputMod
	else
		MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % "No phone number .: " Input
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, justcop, Rohwedder and 155 guests