Strip from string: anything that is not a digit

Just starting out? Need help? Post your questions and find answers here.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Strip from string: anything that is not a digit

Post by StarWarsFan »

I am trying to find a solution for the following:

Given I have StringGadget IN111 where the user is to put in some data in format integer.
I would then get the input result using in$= GetGadgetText(IN111) and put that into variable x for further calculation using x= val(in$)

Let us assume that the user unwillingly hits other keys than he wants so what he enter is 100,g000
He was to put in 100,000, but an extra "g"-character got in the way.
You see where I am going?

I need to strip from that string everything that is not a digit, so I can continue with calculations. Anyone got a solution for this?

Any constructive response greatly appreciated. Thanks for your time, fellows!
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Strip from string: anything that is not a digit

Post by STARGÅTE »

You can use a regular expression:

Code: Select all

Enumeration
	#RegularExpression
EndEnumeration

CreateRegularExpression(#RegularExpression, "\D+") ; The \D stands for all characters except digits 0 to 9
Debug ReplaceRegularExpression(#RegularExpression, "100,g00k0", "") ; Replace the pattern by an empty string.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Strip from string: anything that is not a digit

Post by Little John »

I would silently remove all "," characters.
For wrong characters such as "g", I would issue a warning so that the user can correct the error.
E.g. like this:

Code: Select all

EnableExplicit

#AllowedChars$ = "0123456789,"

Procedure.i CheckIntegerInput (in$)
   Protected char$
   Define.i length, posn
   
   length = Len(in$)
   For posn = 1 To length
      char$ = Mid(in$, posn, 1)
      If FindString(#AllowedChars$, char$) = 0
         MessageRequester("Error", "Invalid character: " + char$)
         ProcedureReturn 0
      EndIf   
   Next   
   
   ProcedureReturn Val(RemoveString(in$, ","))
EndProcedure


Debug CheckIntegerInput("100,g000")
Debug CheckIntegerInput("100000.0")
Debug CheckIntegerInput("100,000")
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: Strip from string: anything that is not a digit

Post by BarryG »

Don't forget that StringGadgets() have a flag called #PB_String_Numeric, to prevent non-digits being typed in. It's in the manual. But this won't stop non-numerics being pasted in from the clipboard.

I personally wait until the user hits Enter on the StringGadget() and then correct anything that is not valid, and use SetGadgetText() to put the corrected text in it. This is a nice convenience feature so that the user doesn't have to re-type anything or dismiss an error message. It reminds them of the format required without actively bothering them.
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Strip from string: anything that is not a digit

Post by Kuron »

BarryG wrote: Sun Oct 29, 2023 12:51 am I personally wait until the user hits Enter on the StringGadget() and then correct anything that is not valid, and use SetGadgetText() to put the corrected text in it. This is a nice convenience feature so that the user doesn't have to re-type anything or dismiss an error message. It reminds them of the format required without actively bothering them.
+1
Best wishes to the PB community. Thank you for the memories. ♥️
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Strip from string: anything that is not a digit

Post by AZJIO »

Use the #EN_CHANGE event, which will be executed when the input field changes, and check using a regular expression. I have to go to work. The problem is standard on the AutoIt3 forum and has been solved 100 times, but I can’t find my example now that takes into account the decimal part of a number.
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: Strip from string: anything that is not a digit

Post by BarryG »

AZJIO wrote: Sun Oct 29, 2023 3:21 amUse the #EN_CHANGE event, which will be executed when the input field changes
Or for cross-platform you can use check for #PB_EventType_Change after EventType().
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Strip from string: anything that is not a digit

Post by AZJIO »

Code: Select all

EnableExplicit

#Window = 0
Enumeration Gadget
	#StrG
	#btn
EndEnumeration
Enumeration RegExp
	#RegExp
	#RegExp2
EndEnumeration



Global tmp$, Text$, Count, start1, end1
Global Dim Res.s(0)

If Not CreateRegularExpression(#RegExp2, "\A\d{1,3}(,\d{3})*(\.\d+)?", #PB_RegularExpression_NoCase)
	Debug RegularExpressionError()
EndIf
If Not CreateRegularExpression(#RegExp, "\A      (\d{1,3}       (,|,\d{1,3}|,\d{3}(\.|\.\d+)?)*?     |\d{1,3}(\.|\.\d+)?)      \z", #PB_RegularExpression_NoCase | #PB_RegularExpression_Extended)
	Debug RegularExpressionError()
EndIf


If OpenWindow(#Window, 0, 0, 220, 100, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	StringGadget(#StrG, 5 , 5, 200, 30, "")
	ButtonGadget(#btn, 10, 50, 200, 30, "Check")
	SetActiveGadget(#StrG)

	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Gadget
				Select EventGadget()
					Case #StrG
						If EventType() = #PB_EventType_Change
							Text$ = GetGadgetText(#StrG)
							If Asc(Text$)
								Count = ExtractRegularExpression(#RegExp, Text$, Res())
								If Count > 0
									tmp$ = Res(0)
								EndIf
								If tmp$ <> Text$
									SendMessage_(GadgetID(#StrG), #EM_GETSEL, @start1, @end1)
									SetGadgetText(#StrG, tmp$)
									start1 - 1
									SendMessage_(GadgetID(#StrG), #EM_SETSEL, start1, start1)


; 									keybd_event_(#VK_CONTROL,0,#KEYEVENTF_EXTENDEDKEY,0)
; 									Delay(10)
; 									keybd_event_(#VK_RIGHT,0,#KEYEVENTF_EXTENDEDKEY,0)
; 									Delay(10)
; 									keybd_event_(#VK_RIGHT,0,#KEYEVENTF_KEYUP | #KEYEVENTF_EXTENDEDKEY,0)
; 									Delay(10)
; 									keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP | #KEYEVENTF_EXTENDEDKEY,0)

								EndIf
							EndIf
						EndIf
					Case #btn
						Text$ = GetGadgetText(#StrG)
						If Asc(Text$)
							Count = ExtractRegularExpression(#RegExp2, Text$, Res())
							If Count > 0
								tmp$ = Res(0)
							EndIf
							If tmp$ <> Text$
								SetGadgetText(#StrG, tmp$)
							EndIf
						EndIf
				EndSelect
			Case #PB_Event_CloseWindow
				CloseWindow(#Window)
				End
		EndSelect
	ForEver
EndIf
Post Reply