Page 3 of 4

Posted: Sat Jan 31, 2004 4:21 pm
by blueznl
regexp in pure (source) or in what?

Posted: Sat Jan 31, 2004 4:35 pm
by FloHimself
blueznl wrote:regexp in pure (source) or in what?
in C

Posted: Sat Jan 31, 2004 7:42 pm
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 :-)

Posted: Tue Feb 03, 2004 1:06 pm
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.

Posted: Fri Feb 06, 2004 5:08 pm
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...

Re: IsAlpha IsNumeric

Posted: Wed Aug 15, 2012 7:09 pm
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

Re: IsAlpha IsNumeric

Posted: Wed Aug 15, 2012 7:40 pm
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

Re: IsAlpha IsNumeric

Posted: Wed Aug 15, 2012 8:50 pm
by luis
Nope.

Re: IsAlpha IsNumeric

Posted: Wed Aug 15, 2012 9:03 pm
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

Re: IsAlpha IsNumeric

Posted: Thu Aug 16, 2012 4:30 am
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

Re: IsAlpha IsNumeric

Posted: Thu Aug 16, 2012 11:31 am
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:

Re: IsAlpha IsNumeric

Posted: Thu Aug 16, 2012 2:16 pm
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


Re: IsAlpha IsNumeric

Posted: Thu Aug 16, 2012 2:45 pm
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

Re: IsAlpha IsNumeric

Posted: Thu Aug 16, 2012 2:59 pm
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:

Re: IsAlpha IsNumeric

Posted: Thu Aug 16, 2012 3:41 pm
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: