IsAlpha IsNumeric

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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... )
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

blueznl wrote:regexp in pure (source) or in what?
in C
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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 :-)
( 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... )
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

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)
Here is the Linux Version of the PBRegExpEx Library.
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

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...
Ta - N
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 130
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: IsAlpha IsNumeric

Post by CONVERT »

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).
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: IsAlpha IsNumeric

Post by VB6_to_PBx »

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
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: IsAlpha IsNumeric

Post by luis »

Nope.
"Have you tried turning it off and on again ?"
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: IsAlpha IsNumeric

Post by ts-soft »

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.
Image
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: IsAlpha IsNumeric

Post by Guimauve »

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
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 :

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")
Best regards
Guimauve
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: IsAlpha IsNumeric

Post by luis »

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 :wink:
"Have you tried turning it off and on again ?"
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsAlpha IsNumeric

Post by SFSxOI »

@Guimauve

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")
here is one for Alpha Numeric using character class "alnum" :

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.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsAlpha IsNumeric

Post by SFSxOI »

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 :wink:

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")
Or... if you just want numeric but also want to detect a ',' in the string, try this:

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
Or... if you just want numeric but also want to detect a ',' in the string and floats, try this:

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.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: IsAlpha IsNumeric

Post by ts-soft »

SFSxOI wrote:The regular expression example in your code above can be simplified by using the "digit" character class:
Your code is only for integer, my code supports floating numbers :wink:
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.
Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsAlpha IsNumeric

Post by SFSxOI »

ts-soft wrote:
SFSxOI wrote:The regular expression example in your code above can be simplified by using the "digit" character class:
Your code is only for integer, my code supports floating numbers :wink:

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
To add other specific characters in case you want to refine the alphanumeric expression just add them after the [:alnum:] and before the following bracket - for example I added a '.' to support floats with this [:alnum:].] (note the '.' after [:alnum:] but before the next bracket)

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")
This work for ya :wink:
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.
Post Reply