IsAlpha IsNumeric
regexp in pure (source) or in what?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
-
- Enthusiast
- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
to translate that to pure might be a bit too much
unless it is short and simple code (although i doubt it is short and simple, i've been puzzling a little over it, and it is *not* that easy, at least not for a peabrain like me)
i'll think it over, but i might have to pass on this one, even though i am somewhat intrigued

i'll think it over, but i might have to pass on this one, even though i am somewhat intrigued

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
-
- Enthusiast
- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
Here is the Linux Version of the PBRegExpEx Library.naw wrote:wow! Brilliant - I bet there's only a Lib for Windows, though, Linux would be very nice. (I cant check reelmedia because the reelmedia server is refusing connections again)
Wow!!! (again) PB and those who know it well are amazing.
Ok, we all agree that language itself is great for casual users (me) and the PB Professors as well. But, its the general community that go out of their way to give something without expectation of anything in return that makes PB a bit *special*.
So now PB has Regular Expressions (compatible Libraries for Windows & Linux) which really widens its capabilities.
- If only PB Linux & windows had support for STDIN / STDERR as well - <sigh>
Thanks guys for doing this work...
Ok, we all agree that language itself is great for casual users (me) and the PB Professors as well. But, its the general community that go out of their way to give something without expectation of anything in return that makes PB a bit *special*.
So now PB has Regular Expressions (compatible Libraries for Windows & Linux) which really widens its capabilities.
- If only PB Linux & windows had support for STDIN / STDERR as well - <sigh>

Thanks guys for doing this work...
Ta - N
Re: IsAlpha IsNumeric
Code: Select all
Procedure.b isnumeric_jc(Pstring$)
Define wres.b
If Pstring$ = Left(StrD(ValD(Pstring$)), Len(Pstring$))
wres.b = 1
Else
wres.b = 0
EndIf
ProcedureReturn wres.b
EndProcedure
PureBasic 6.20 beta 2 (x64) | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled.
Come back to 6.11 LTS 64 bits because of an issue with #PB_ComboBox_UpperCase in ComboBoxGadget() (Oct. 10, 2024).
Come back to 6.11 LTS 64 bits because of an issue with #PB_ComboBox_UpperCase in ComboBoxGadget() (Oct. 10, 2024).
- VB6_to_PBx
- Enthusiast
- Posts: 627
- Joined: Mon May 09, 2011 9:36 am
Re: IsAlpha IsNumeric
CONVERT wrote:Code: Select all
Procedure.b isnumeric_jc(Pstring$) Define wres.b If Pstring$ = Left(StrD(ValD(Pstring$)), Len(Pstring$)) wres.b = 1 Else wres.b = 0 EndIf ProcedureReturn wres.b EndProcedure
will the above work correctly for non-US keyboard settings like COMMA vs Decimal point in numeric inputs ?
Debug isnumeric_jc("1.23") ; USA keyboard
or
Debug isnumeric_jc("1,23") ; Countries that use COMMA instead of decimal point
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: IsAlpha IsNumeric
Code: Select all
Procedure IsNumeric(numstr.s)
Protected Result, Pattern.s = "^[-+]?[0-9]*\.?[0-9]+$"
Protected RegEx = CreateRegularExpression(#PB_Any, Pattern)
If RegEx
Result = MatchRegularExpression(RegEx, numstr)
FreeRegularExpression(RegEx)
ProcedureReturn Result
EndIf
EndProcedure
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: IsAlpha IsNumeric
Nice but for a single call it's not apparent but if your procedure has been called many times(inside a large loop for example), creating and freeing the regular expression has a huge impact on the speed. It's better to create the Regular Expression once and let the program extinction free it. Like this :ts-soft wrote:Code: Select all
Procedure IsNumeric(numstr.s) Protected Result, Pattern.s = "^[-+]?[0-9]*\.?[0-9]+$" Protected RegEx = CreateRegularExpression(#PB_Any, Pattern) If RegEx Result = MatchRegularExpression(RegEx, numstr) FreeRegularExpression(RegEx) ProcedureReturn Result EndIf EndProcedure
Code: Select all
Procedure IsNumeric(numstr.s)
Protected Result
Static RegEx
If RegEx = #Null
RegEx = CreateRegularExpression(#PB_Any, "^[-+]?[0-9]*\.?[0-9]+$")
EndIf
If RegEx <> #Null
Result = MatchRegularExpression(RegEx, numstr)
ProcedureReturn Result
EndIf
EndProcedure
Debug IsNumeric("")
Debug IsNumeric("0123456789")
Debug IsNumeric("1234a")
Guimauve
Re: IsAlpha IsNumeric
Yes, you can save some time that way, but there is the disadvantage the regex is kept allocated in memory for the life of the program. Not saying is a problem, just pointing that out.
Uhm... anyway if the regex was in response to VB6_to_PBx question this still doesn't work.
Debug IsNumeric("0,1") gives 0.
Probably a '\,' missing in the regex alternative to the '\.', maybe [\,\.] ?
But I leave this to someone who loves regex more than me (and certainly more skilled with them) since I could end up doing more damage than good
Uhm... anyway if the regex was in response to VB6_to_PBx question this still doesn't work.
Debug IsNumeric("0,1") gives 0.
Probably a '\,' missing in the regex alternative to the '\.', maybe [\,\.] ?
But I leave this to someone who loves regex more than me (and certainly more skilled with them) since I could end up doing more damage than good

"Have you tried turning it off and on again ?"
Re: IsAlpha IsNumeric
@Guimauve
The regular expression example in your code above can be simplified by using the "digit" character class:
here is one for Alpha Numeric using character class "alnum" :
The regular expression example in your code above can be simplified by using the "digit" character class:
Code: Select all
Procedure IsNumeric(in_str.s)
rex_isnumber = CreateRegularExpression(#PB_Any,"^[[:digit:]]+$") ; Any digit 0-9
is_number.b = MatchRegularExpression(rex_isnumber, in_str)
ProcedureReturn is_number
EndProcedure
Debug IsNumeric("0123456789")
Debug IsNumeric("")
Debug IsNumeric("0123456789a")
Code: Select all
Procedure IsAlphaNumeric(in_str.s)
rex_isAlphaNumeric = CreateRegularExpression(#PB_Any,"^[[:alnum:]]+$") ; AlphaNumeric
is_AlphaNumeric.b = MatchRegularExpression(rex_isAlphaNumeric, in_str)
ProcedureReturn is_AlphaNumeric
EndProcedure
Debug IsAlphaNumeric("qwertyuiopoasdfghjklzxcvbnm1234567890") ; will return True
Debug IsAlphaNumeric("qwertyuiopoasdfghjklzxcvbnm1234567890/") ; will return False because the '/' is not an alpha numeric character
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: IsAlpha IsNumeric
luis wrote:Yes, you can save some time that way, but there is the disadvantage the regex is kept allocated in memory for the life of the program. Not saying is a problem, just pointing that out.
Uhm... anyway if the regex was in response to VB6_to_PBx question this still doesn't work.
Debug IsNumeric("0,1") gives 0.
Probably a '\,' missing in the regex alternative to the '\.', maybe [\,\.] ?
But I leave this to someone who loves regex more than me (and certainly more skilled with them) since I could end up doing more damage than good
luis;
Try this:
Code: Select all
Procedure IsAlphaNumeric(in_str.s) ; will support foats and a ','
rex_isAlphaNumeric = CreateRegularExpression(#PB_Any,"^[[:alnum:].,]+$") ; AlphaNumeric
is_AlphaNumeric.b = MatchRegularExpression(rex_isAlphaNumeric, in_str)
ProcedureReturn is_AlphaNumeric
EndProcedure
Debug IsAlphaNumeric("qwertyuiopoasdfghjklzxcvbnm1234567890") ; will return True
Debug IsAlphaNumeric("qwertyuiopoasdfghjklzxcvbnm1234567890/") ; will return False because the '/' is not an alpha numeric character
Debug IsAlphaNumeric("123,4567890")
Debug IsAlphaNumeric("123.4567890")
Code: Select all
Procedure IsNumeric(in_str.s) ; with comma detection
rex_IsNumeric = CreateRegularExpression(#PB_Any,"^[[:digit:],]+$") ; Any digits 0-9 and a comma
Is_Numeric.b = MatchRegularExpression(rex_IsNumeric, in_str)
ProcedureReturn Is_Numeric
EndProcedure
Code: Select all
Procedure IsNumeric(in_str.s) ; with comma detection
rex_IsNumeric = CreateRegularExpression(#PB_Any,"^[[:digit:].,]+$") ; Any digits 0-9 and a comma
Is_Numeric.b = MatchRegularExpression(rex_IsNumeric, in_str)
ProcedureReturn Is_Numeric
EndProcedure
Last edited by SFSxOI on Thu Aug 16, 2012 4:42 pm, edited 7 times in total.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: IsAlpha IsNumeric
Your code is only for integer, my code supports floating numbersSFSxOI wrote:The regular expression example in your code above can be simplified by using the "digit" character class:

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: IsAlpha IsNumeric
ts-soft wrote:Your code is only for integer, my code supports floating numbersSFSxOI wrote:The regular expression example in your code above can be simplified by using the "digit" character class:
Here you go, alpha numeric regular expression with character class "alnum" for alpha numeric that supports floats
Code: Select all
Procedure IsAlphaNumeric(in_str.s)
rex_isAlphaNumeric = CreateRegularExpression(#PB_Any,"^[[:alnum:].]+$") ; AlphaNumeric
is_AlphaNumeric.b = MatchRegularExpression(rex_isAlphaNumeric, in_str)
ProcedureReturn is_AlphaNumeric
EndProcedure
Debug IsAlphaNumeric("qwertyuiopoasdfghjklzxcvbnm1234567890") ; will return True
Debug IsAlphaNumeric("qwertyuiopoasdfghjklzxcvbnm1234567890/") ; will return False because the '/' is not an alpha numeric character
Debug IsAlphaNumeric("123.4567890") ; returns true for a floating number
You can also do just intergers (as you pointed out for the one example) but still support floats, for example:
Code: Select all
Procedure IsNumeric(in_str.s) ; integers and floats
rex_isnumber = CreateRegularExpression(#PB_Any,"^[[:digit:].]+$") ; Any digit 0-9 and floats
is_number.b = MatchRegularExpression(rex_isnumber, in_str)
ProcedureReturn is_number
EndProcedure
Debug IsNumeric("1.0")

Last edited by SFSxOI on Sat Aug 18, 2012 9:15 am, edited 5 times in total.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.