Jump to content

Need help with Regex to validate a UUID


nikink
 Share

Recommended Posts

Hi all, it's been a while since I last used regular expressions and I find myself out of time to experiment with this particular issue, so I throw myself upon your mercy and expertise. :)

I am looking to create a function that will say whether or not a supplied string is a valid UUID or not.

Local $sTestF = '4C4C4544-004A-4C10-8054-B7C04F46343'
Local $sTestT = '4C4C4544-004A-4C10-8054-B7C04F463432'

ConsoleWrite('False = ' & _IsValidUUID($sTestF) & @CRLF)
ConsoleWrite('True = ' & _IsValidUUID($sTestT) & @CRLF)

Func _IsValidUUID($sUUID)
    ;[\p{XDigit}]{8}-[\p{XDigit}]{4}-[34][\p{XDigit}]{3}-[89ab][\p{XDigit}]{3}-[\p{XDigit}]{12}
    ; Test UUID = '4C4C4544-004A-4C10-8054-B7C04F463432'


    Local $sRegExp = '([:xdigit:]){8}\-([:xdigit:]){4}\-([34])([:xdigit:]){3}\-([89ab])([:xdigit:]){3}\-([:xdigit:]){12}'

    ConsoleWrite(StringRegExp($sUUID, $sRegExp) & @CRLF)

    Local $Result = StringRegExp($sUUID, $sRegExp)
    ConsoleWrite($Result & @CRLF)
    If @error Then
        ConsoleWrite('Error: [' & @error & ']' & @CRLF)
        Return 'False'
    Else
        ConsoleWrite('Error2: [' & @error & ']' & @CRLF)
        Return 'True'
    EndIf

EndFunc

In the line under the Function call, you'll see the regex I found to do this from a google search. That was my starting point, and I'm trying to get it to work in Au3 and failing miserably.

$sTestF is a known invalid String

$sTestT is a known valid String

Everything I've tried so far has produced the same results for both. :'(

Any help you could provide me is greatly appreciated. Thanks for your time!

Link to comment
Share on other sites

Try this one, with [:xdigit:] replaced by [[:xdigit:]]

Local $sRegExp = '[[:xdigit:]]{8}-[[:xdigit:]]{4}-[34][[:xdigit:]]{3}-[89abAB][[:xdigit:]]{3}-[[:xdigit:]]{12}'

Capturing groups "(....)" seems to be not necassary so I removed them

Link to comment
Share on other sites

Try this.

Local $sTestF = '4C4C4544-004A-4C10-8054-B7C04F46343' ; False
Local $sTestT = '4C4C4544-004A-4C10-8054-B7C04F463432' ; True
Local $sTestR = '8ee62002-675f-4599-ad48-712fe45b986f' ; True

ConsoleWrite($sTestF & ' IsValidUUID = ' & _IsValidUUID($sTestF) & @CRLF)
ConsoleWrite($sTestT & ' IsValidUUID = ' & _IsValidUUID($sTestT) & @CRLF)
ConsoleWrite($sTestR & ' IsValidUUID = ' & _IsValidUUID($sTestR) & @CRLF)


Func _IsValidUUID($sUUID)
    Local $sRegExp = '[[:xdigit:]]{8}-[[:xdigit:]]{4}-[34][[:xdigit:]]{3}-[89abAB][[:xdigit:]]{3}-[[:xdigit:]]{12}'
    Return StringRegExp($sUUID, $sRegExp) = 1 ; The " = 1" converts 1 or 0 to True or False.
EndFunc   ;==>_IsValidUUID

#cs ; Returns:-
4C4C4544-004A-4C10-8054-B7C04F46343 IsValidUUID = False
4C4C4544-004A-4C10-8054-B7C04F463432 IsValidUUID = True
8ee62002-675f-4599-ad48-712fe45b986f IsValidUUID = True
#ce

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...