nikink

Need help with Regex to validate a UUID

5 posts in this topic

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!

Share this post


Link to post
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

1 person likes this

Share this post


Link to post
Share on other sites

Oh wow. The ol' double square bracket trick... Thankyou so much! That seems to be perfect! You even caught the missing AB in the [89ab] set. :)

Share this post


Link to post
Share on other sites

@Nikink It's probably only a producer code but the code in post 1 will always return true because you are checking for @error after consolewrite and not for any StringRegExp errors

Share this post


Link to post
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

 

Share this post


Link to post
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

  • Similar Content

    • nend
      By nend
      This is a program that I made to help my self learn better regular expressions.
      There are a lot of other programs/website with the similar functions.
      But the main advantage of this program is that you don't have to click a button after every changes.
      The program detected changes and react on it.
      Function:
      Match Match of arrays Match and replace Load source data from website Load source data from a website with GET/POST Load text data from file Clear fields Export and Import settings (you can finish the expression a other time, just export/import it) Cheat sheet Generate AutoIt code The source code is not difficult and I think most user will understand it.
      In the zip file there are 2 export files (POST and a reg back example), you can drag and drop these files on the gui to import them.
      Download Regex Toolkit here Regex toolkit.zip
      EDIT: Updated to version V1.2.0
      Changes are:
      Expand and collapse of the cheat sheet (Thanks to Melba23 for the Guiextender UDF) Usefull regular expressions websites links included in the prgram Text data update time EDIT: Updated to version V1.3.0
      Changes are:
       Automatic generate AutoIt code  Icons on the tab  Few minor bug fixes EDIT: Updated to version V1.4.0
      Changes are:
      Link to AutoIt regex helpfile If the regular expression has a error than the text becomes red Option Offset with Match and array of Matches Option Count with Match and replace Some small minor bug fixed
    • ISI360
      By ISI360
      Hi!

      I need a little bit help from some RegEx experts please:
      I would make my ISN AutoIt Studio faster when generating the scripttree. And what would be better to do this via regex?
      Problem is i am not really good at this regex stuff. So maybe someone could help me here.
       
      The challange is to get all Global Variables from a script via RegEx in a Array.
      Here is a example script with some tests:
      Global $Var1 = 1234 Local $Local_Var = 1234 $Ignore_me_too = 1234 Global $Var2 = 1234, $var3 = 1242 Global $ahIcons[30], $ahLabels[30] Global Const $Var4 = iniread($inivar1,"jj","jj","") , $var5= iniread($inivar2,"jj","jj","") Global $Var_String = "was" Global $Array_Test[16] = [1,15,16,0,31,15,25,15,25,30,8,30,8,15,1,15] Global Enum $MARGIN_SCRIPT_NUMBER = 0, $MARGIN_SCRIPT_ICON, $MARGIN_SCRIPT_FOLD Global Const $Delim = '\', $Delim1 = '|' Global $hard1 = "a", _ $hard2 = "b", _ $hard3 = "c"  
      The returning array should look like this:
      $Var1 $Var2 $var3 $Var4 $var5 $Var_String $Array_Test $MARGIN_SCRIPT_NUMBER $MARGIN_SCRIPT_ICON $MARGIN_SCRIPT_FOLD $Delim $Delim1 $hard1 $hard2 $hard3  
      I already made some success with a expression i found in the SciTE Jump Tool:  (\$\w+)(?:[\h\[.=+*/^,)\-])?
      This nearly returns the perfect results. But it does not check if it´s a global variable (with the const and enum options) and also returns variables in commands (for example $inivar1)
      I also found this regex: (?im:^(?=Global|Const|Enum|Static)(?:Global)?\h*(?:Const|Enum|Static)?(?:(?<=Enum)\h+Step\h+[+*-]\d+)?\h*)([^\r\n .\=]+)
      This returns also usefull results...but trying to understand this explodes my head

      Maybe someone can help me here?
      Thanks in advance!
    • TheAutomator
      By TheAutomator
      Can anyone tell me why this isn't working?..
      #include <array.au3> $regexp = StringRegExp("test 'a b c'", "'([^']|'')*'|\S+", 3) _ArrayDisplay($regexp) trying to split this "test 'a b c'  'some other '' test'' ...'" into:
      0: test
      1: 'a b c'
      2: ...
      but it gives me:
      0: test
      1: c
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • tezhihi
      By tezhihi
      I have a file (see attached file) with a string all line and this problem on here is I want to separate all $00:, $03:, $10:, $20:, $25:, $30:, $40:, $45:, $110:, $115:, $120: and $T. It's mean that each $ with value start a new line ( a new paragraph). I tried with Regular Expression in notepad++ ex:
      Find ($00:, $01:, $03: and so on) with regex (\$)([0-9]+): and replace is \r\n\1\2 (I think \r\n is @CRLF (not sure :() ) Find $T with regex (\$T)(.*?)(\$T) and replace is \1\2\r\n\3 When I try these regex to replace in notepad on StringRegexReplace the results is incorrect . I have read some example simple about regex. Please advise me how to do that with some example on autoit . The result will be in attached photo. Thanks 
      ahihi.txt