Posted: Sat Jan 31, 2004 4:21 pm
regexp in pure (source) or in what?
http://www.purebasic.com
https://www.purebasic.fr/english/
in Cblueznl wrote:regexp in pure (source) or in what?
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)
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
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
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 :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")
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
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
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
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:
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:
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
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")