Check if a UUID is valid without using regexes

44

1

Given a string input, write a program that prints a truthy value to STDOUT or equivalent if the input is a valid UUID, without using regexes.

A valid UUID is

32 hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens).

Source

Test Cases

0FCE98AC-1326-4C79-8EBC-94908DA8B034
    => true
00000000-0000-0000-0000-000000000000
    => true
0fce98ac-1326-4c79-8ebc-94908da8b034
    => true
0FCE98ac-1326-4c79-8EBC-94908da8B034
    => true

{0FCE98AC-1326-4C79-8EBC-94908DA8B034}
    => false (the input is wrapped in brackets)
0GCE98AC-1326-4C79-8EBC-94908DA8B034
    => false (there is a G in the input)
0FCE98AC 1326-4C79-8EBC-94908DA8B034
    => false (there is a space in the input)
0FCE98AC-13264C79-8EBC-94908DA8B034
    => false (the input is missing a hyphen)
0FCE98AC-13264-C79-8EBC-94908DA8B034
    => false (the input has a hyphen in the wrong place)
0FCE98ACD-1326-4C79-8EBC-94908DA8B034
    => false (one of the groups is too long)
0FCE98AC-1326-4C79-8EBC-94908DA8B034-
    => false (has a trailing hyphen)
0FCE98AC-1326-4C79-8EBC-94908DA8B034-123
    => false (too many groups)
0FCE98AC13264C798EBC94908DA8B034
    => false (there is no grouping)

Rules

  • Regular Expressions are not allowed
  • Literal pattern matching which is like a regex is not allowed. For example, using [0-9a-fA-F] or other hexadecimal identifiers (we'll call this n) and then matching nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn or n[8]-n[4]-n[4]-n[4]-n[12] is not allowed
  • The input may either be taken from STDIN or as an argument to a function
  • The input is case insensitive
  • It is safe to assume that the input will not contain linefeeds or newlines.
  • The input may contain any printable ASCII characters (spaces included)
  • A truthy value must be printed to STDOUT or equivalent if the input is a valid uuid
  • A falsey value must be printed to STDOUT or equivalent if the input is not a valid uuid
  • If using a function, instead of using STDOUT, the output can be the return value of the function
  • The truthy/falsey value cannot be printed to STDERR.
  • Standard loopholes apply
  • This is , so the shortest program in bytes wins. Good luck!

Leaderboard

This is a Stack Snippet that generates both a leaderboard and an overview of winners by language.

To ensure your answer shows up, please start your answer with a headline using the following Markdown template

## Language Name, N bytes

Where N is the size, in bytes, of your submission

If you want to include multiple numbers in your header (for example, striking through old scores, or including flags in the byte count), just make sure that the actual score is the last number in your header

## Language Name, <s>K</s> X + 2 = N bytes

var QUESTION_ID=66496;var OVERRIDE_USER=20634;function answersUrl(e){return"//api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"//api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(-?\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

Jojodmo

Posted 2015-12-13T19:40:23.617

Reputation: 1 569

32

Poor Retina. D:

– BrainSteel – 2015-12-13T19:44:47.440

Related – FryAmTheEggman – 2015-12-13T20:44:17.930

8

Just for reference, I can come up with a 28-byte Retina solution. (So not a crazy advantage over the golfing languages although it would currently be leading.)

– Martin Ender – 2015-12-13T22:53:00.807

5

Are Lua's patterns allowed? They are certainly not regular expressions.

– manatwork – 2015-12-14T09:50:41.080

You have to make language list case insensitive... – Qwertiy – 2015-12-14T20:28:31.590

What should the output be for 0FCE98AC13264C798EBC94908DA8B034? – Jacob Krall – 2015-12-15T16:27:07.567

Expanding the question - what should the output be for input.length!=36? e.g. {dddddddd-dddd-dddd-dddd-dddddddddddd}, {0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}} – Jacob Krall – 2015-12-15T16:29:01.273

1@JacobKrall I'm pretty sure it's "falsy" to all of your questions. I think the challenge is fairly clear that only strings of the form \h{8}-\h{4}-\h{4}-\h{4}-\h{12} (where \h is a hexadecimal digit) are valid. – Martin Ender – 2015-12-15T17:24:09.370

@MartinBüttner: I agree, but there are currently several answers which use built-in uuid constructors, which accept other formats. – Jacob Krall – 2015-12-15T17:26:36.857

@MartinBüttner If that's indeed the case, the question should be edited to explicitly state that it is expecting canonical-form parsing, and not just valid-UUID parsing. 0FCE98AC13264C798EBC94908DA8B034 is a valid UUID, just not in canonical form. – AdmBorkBork – 2015-12-15T18:52:39.840

@Jojodmo Those test cases, particularly the one without hyphens, were added after a majority of answers were posted, mine included. Hence my asking for clarification. – AdmBorkBork – 2015-12-15T19:40:28.243

Are scanf()-style format specifiers also disgualified as being like a regex?

– Digital Trauma – 2015-12-15T22:08:58.547

@Jojodmo I guess that disqualifies both my answers then :(. I'm still leaving them up though as I think this was not clear in the question as originally written. – Digital Trauma – 2015-12-15T23:48:23.453

@What about APIs like isxdigit() which operate on a single character at a time?

– Digital Trauma – 2015-12-15T23:49:03.397

@DigitalTrauma isxdigit would be fine. Also, I'm pretty sure you posted your answer before I added the part about literal pattern matching, so feel free to leave it up, just make sure you say that it is not competative – Jojodmo – 2015-12-15T23:51:25.317

Can answers use .NET languages and call System.Guid.TryParse? – phoog – 2015-12-17T05:52:45.313

@phoog Yes, but it has to only accept the format 8-4-4-4-12 – Jojodmo – 2015-12-17T06:40:36.277

Answers

15

CJam, 31 30 29 bytes

8 4__C]Nf*'-*qA,s'G,_el^+Ner=

Run all test cases here.

Explanation

Instead of pattern matching the input directly, we're first transforming it to a simpler form which can be easily compared against a single pattern string.

8 4__C] e# Push the array of segment lengths, [8 4 4 4 12].
Nf*     e# Turn that into strings of linefeeds of the given length.
'-*     e# Join them by hyphens, giving "NNNNNNNN-NNNN-NNNN-NNNN-NNNNNNNNNNNN".
q       e# Read the input.
A,s     e# Push the string "0123456789".
'G,_el^ e# Push the string "ABCDEFabcdef".
+       e# Concatenate the two strings.
N       e# Push a linefeed.
er      e# Replace all hexadecimal digits with linefeeds.
=       e# Check for equality with the pattern string.

Martin Ender

Posted 2015-12-13T19:40:23.617

Reputation: 184 808

23

JavaScript ES6, 73 55 56 chars

s=>s.split`-`.map(x=>x.length+`0x${x}0`*0)=="8,4,4,4,12"

The previous 55 chars version has a problem with trailing spaces in group:

s=>s.split`-`.map(x=>x.length+("0x"+x)*0)=="8,4,4,4,12"
// "00000000-0000-0000-000 -000000000000" true

Test:

f=s=>s.split`-`.map(x=>x.length+`0x${x}0`*0)=="8,4,4,4,12"
;`0FCE98AC-1326-4C79-8EBC-94908DA8B034
0fce98ac-1326-4c79-8ebc-94908da8b034
0FCE98ac-1326-4c79-8EBC-94908da8B034
0GCE98AC-1326-4C79-8EBC-94908DA8B034
0FCE98AC-13264C79-8EBC-94908DA8B034
0FCE98AC-13264-C79-8EBC-94908DA8B034
0FCE98ACD-1326-4C79-8EBC-94908DA8B034
0FCE98AC-1326-4C79-8EBC-94908DA8B034-123
00000000-0000-0000-0000-000000000000
D293DBB2-0801-4E60-9141-78EAB0E298FF
0FCE98AC-1326-4C79-8EBC-94908DA8B034-
00000000-0000-0000-000 -000000000000`.split(/\n/g).every(s=>f(s)==/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i.test(s))

Qwertiy

Posted 2015-12-13T19:40:23.617

Reputation: 2 697

Awesome (and withering for me) +1 – edc65 – 2015-12-13T23:06:17.043

@edc65, what do you mean by "withering"? – Qwertiy – 2015-12-13T23:29:57.307

Awesome abuse of implicit casting +1 – Downgoat – 2015-12-14T01:00:22.117

4scornful, contemptuous, scathing, stinging, devastating, humiliating, mortifying - (comparing your answer to mine) – edc65 – 2015-12-14T07:42:47.790

11

PowerShell, 29 21 84 49 37 Bytes

param($g)@{36=$g-as[guid]}[$g.length]

Many thanks to the folks in the comments assisting with this golfing to keep up with the changing rules -- TessellatingHeckler, iFreilicht, Jacob Krall, and Joey. Please see the edit history for revisions and older versions.

This revision takes input as $g, then creates a new hash table @{} with one element, index 36 is set equal to $g-as[guid]. This uses the built-in -as operator to attempt conversion between two .NET data types -- from [string] to [guid]. If the conversion is successful, a [guid] object is returned, else $null is returned. This portion ensures that the input string is a valid .NET GUID.

The next step is to index into the hash table with [$g.length]. If $g is not exactly 36 characters in length, the hash table will return $null, which will be output as a falsey value. If $g is 36 characters in length, then the result of the .NET call will be output. If $g is not a valid .NET GUID (in any form), then it will output $null as a falsey value. Otherwise it will output a .NET GUID object as a truthy value - the only way that can be output is if it matches the requested format of the challenge.

Examples

Here I am encapsulating the script call in parens and explicitly casting as a Boolean for clarity.

PS C:\Tools\Scripts\golfing> [bool](.\check-if-a-uuid-is-valid.ps1 '0FCE98AC-1326-4C79-8EBC-94908DA8B034')
True

PS C:\Tools\Scripts\golfing> [bool](.\check-if-a-uuid-is-valid.ps1 '0FCE98AC-1326-4C79-8EBC-94908DA8B034D')
False

PS C:\Tools\Scripts\golfing> [bool](.\check-if-a-uuid-is-valid.ps1 '0FCE98AC13264C798EBC94908DA8B034')
False

AdmBorkBork

Posted 2015-12-13T19:40:23.617

Reputation: 41 581

4I'll throw in !!($args[0]-as[guid]) at 21 bytes. – TessellatingHeckler – 2015-12-15T00:01:55.317

2Couldn't you just save 4 bytes by leaving out the !!()? As the values $NULL and [guid] are mutually exclusive, they qualify to represent truthey and falsey values, don't they? Anyway, great way to convert to boolean, love the solution! – iFreilicht – 2015-12-15T14:42:50.933

@iFreilicht that is a point; looking at the linked "truthy/falsey interpretation" post - I agree it looks valid. – TessellatingHeckler – 2015-12-15T16:43:52.277

1This solution incorrectly returns True for 0FCE98AC13264C798EBC94908DA8B034, which has no hyphens – Jacob Krall – 2015-12-15T16:45:35.137

@TimmyD: I disagree with your interpretation of the question, even as-of yesterday. The revision you link to contains the verbatim text "A valid UUID is 32 hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens)." Therefore we can infer that, for the purposes of this question, any other representation is not a valid UUID. – Jacob Krall – 2015-12-15T18:30:32.220

@JacobKrall: That's something that can only be resolved by resorting to try{!![guid]::parseexact("$args",'d')}catch{0}, I think. – Joey – 2015-12-16T07:41:35.167

@TessellatingHeckler That is a really clever way of "counting" the length of the string, seems like a really good solution. – iFreilicht – 2015-12-16T08:29:19.500

1@TessellatingHeckler Nope, too good to be true. Appending a digit, such as 0FCE98AC-1326-4C79-8EBC-94908DA8B034D (extra D on the end) returns falsey $TRUE, since it just trims the offending digit away and the first 36 characters are valid. – AdmBorkBork – 2015-12-16T14:12:20.123

@Joey Thanks for the assist. I combined your suggestion with iFreilicht and golfed a few more bytes. – AdmBorkBork – 2015-12-16T14:34:13.173

@TimmyD: I think TesselatingHeckler had a better solution already. It's just that I haven't read that comment when I wrote my own. – Joey – 2015-12-16T14:55:20.090

@TimmyD annoying edge case, but good catch. Still, 49 bytes cannot stand, how about param($g)@{36=$g-as[guid]}[$g.Length] at 37 bytes? All 5 formats .Net handles will convert - but only the one we care about will have the right length to index the hashtable and appear as script output. Incorrect lengths will get a hashtable $null default return. Incorrect inputs of the right length can't be valid GUIDs so they will convert to $null, lookup in the hashtable and have $null output as well. – TessellatingHeckler – 2015-12-16T19:43:56.920

9

Emacs Lisp, 236 Bytes

(lambda(s)(and(eq(string-bytes s)36)(let((l(string-to-list s))(i 0)(h '(8 13 18 23))(v t))(dolist(c l v)(set'v(and v(if(member i h)(and v(eq c 45))(or(and(> c 47)(< c 58))(and(> c 64)(< c 91))(and(> c 96)(< c 123))))))(set'i(+ i 1))))))

Ungolfed:

(lambda (s)
  (and (eq (string-bytes s) 36) ; check length
       (let ((l (string-to-list s))
             (i 0)
             ; location of hyphens
             (h '(8 13 18 23))
             (v t))
         (dolist (c l v)
           (set 'v (and v (if (member i h)      ; check if at hyphen position
                              (and v (eq c 45)) ; check if hyphen
                            (or (and (> c 47) (< c 58))      ; check if number
                                (and (> c 64) (< c 91))      ; check if upper case letter
                                (and (> c 96) (< c 123)))))) ; check if lower case letter
           (set 'i (+ i 1)))))) ; increment

Lord Yuuma

Posted 2015-12-13T19:40:23.617

Reputation: 587

8

Due to changes to the rules, this answer is no longer competitive :(

C, 98

main(a,n){printf("%d",scanf("%8x-%4hx-%4hx-%4hx-%4hx%8x%n%c",&a,&a,&a,&a,&a,&a,&n,&a)==6&&n==36);}

Mostly fairly self explanatory. The %n format specifier gives the number of bytes read so far, which should be 36. scanf() returns the number of matched items, which should be 6. The final %c should not match anything. If it does, then there is trailing text, and scanf() will return 7.

Compile with -w to suppress the pesky warnings (there are several).

Digital Trauma

Posted 2015-12-13T19:40:23.617

Reputation: 64 644

6

JavaScript ES6, 70 83

NOTE thx to @Qwertiy for finding a bug (and suggesting some improvements and fixes)

Thx @CᴏɴᴏʀO'Bʀɪᴇɴ 2 bytes saved

Other 9 bytes saved simplifiying the length check (the complex way was shorter in the first draft, but not now)

u=>u.split`-`.every((h,l,u)=>u[4]&&-`0x${h}1`&&h.length-'40008'[l]==4)

Explained

u=>u.split`-` // make an array splitting at '-'
.every( // for every element the following must be true
 (h,l,u)=> // h is the element, l is the index, u is the whole array
 u[4] // element 4 must be present (at least 5 element in array)
 && -`0x${h}1` // element must be a valid hex string with no extraneous blanks (else NaN that is falsy)
 // get requested length from index (8,4,4,4,12 sub 4 to put in 1 char)
 // a 6th elements will be rejected as undefined != 4
 && h.length-'40008'[l]==4// then check element length
)

Test snippet

f=u=>u.split`-`.every((h,l,u)=>u[4]&&-`0x${h}1`&&h.length-'40008'[l]==4)

console.log=x=>O.innerHTML+=x+'\n'

;[
  ['0FCE98AC-1326-4C79-8EBC-94908DA8B034',true],
  ['0fce98ac-1326-4c79-8ebc-94908da8b034',true],
  ['0FCE98ac-1326-4c79-8EBC-94908da8B034',true],
  ['00000000-0000-0000-0000-000000000000', true],
  ['ffffffff-ffff-ffff-ffff-ffffffffffff', true],
  ['0GCE98AC-1326-4C79-8EBC-94908DA8B034',false],
  ['0FCE98AC-13264C79-8EBC-94908DA8B034',false],
  ['0FCE98AC-13264-C79-8EBC-94908DA8B034',false],
  ['0FCE98ACD-1326-4C79-8EBC-94908DA8B034',false],
  ['0FCE98AC-1326-4C79-8EBC',false],
  ['0FCE98AC-1326-4C79-8EBC-94908DA8B034-',false],
  ['00000000-0000-0000-000 -000000000000', false],
  ['0FCE98AC-1326-4C79-8EBC-94908DA8B034-123',false],
].forEach(x=>{
  var t=x[0], r=f(t), k=x[1]
  console.log('Test '+t+' result '+r+(r==k?' ok':' fail'))
})
<pre id=O></pre>

edc65

Posted 2015-12-13T19:40:23.617

Reputation: 31 086

-1-('0x'+h) => 1/('0x'+h) – Qwertiy – 2015-12-13T23:27:26.187

Same problem as in my previous version: true for 00000000-0000-0000-000 -000000000000 – Qwertiy – 2015-12-14T20:23:43.053

No. But y've added a wrong test. Leading space is not a problem, but trailing one is, as a string is trimmed when converting to number. See my comment above with a test. – Qwertiy – 2015-12-14T20:37:11.690

'00000000-0000-0000-000 -000000000000' now I see it @Qwertiy I missed the 3 zeros instead of 4 – edc65 – 2015-12-14T20:43:13.357

'40008x' => '40008' as undefined is not equal to 4. – Qwertiy – 2015-12-14T20:57:16.867

@Qwertiy no matter how hard you try, I can't reach your score – edc65 – 2015-12-14T21:01:17.143

By the way, I don't think it was a bug as explicit rule about spaces have appeared later. – Qwertiy – 2015-12-14T21:01:21.667

http://codegolf.stackexchange.com/a/63494/32091 sometimes the length becomes significantly reduced :) – Qwertiy – 2015-12-14T21:03:15.187

1@Stefnotch i don't agree. Outside the every call u is a string, not an array – edc65 – 2015-12-15T19:40:34.533

5

Due to changes to the rules, this answer is no longer competitive :(

Pure Bash (no external utilities), 78

printf -vv %8s-%4s-%4s-%4s-%12s
p=${v// /[[:xdigit:]]}
[ "$1" -a ! "${1/$p}" ]

Takes input from the command line.

  • The printf builds the following string - - - -.
  • The p= line transforms this to the following pattern: [[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]-[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]. Note this looks an awful lot like a regular expression. However, it is not in this context. It is a pattern for shell pattern matching. This is similar in concept to a regular expression, but is a different construct (and syntax).
  • The last line checks if
    • the input is non-empty
    • if pulling the pattern out of the input string produces an empty string

Idiomatic to shell, a return code of 0 indicates success/TRUE and 1 indicates failure/FALSE. The return code may be inspected with echo $? after running the script.

Digital Trauma

Posted 2015-12-13T19:40:23.617

Reputation: 64 644

1The shell pattern matching may not follow the syntax of regex, but the character class definitely uses the definition and syntax from POSIX regex. Anyway, it's up to OP to decide whether it's acceptable. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ – 2015-12-14T03:38:33.430

1

@n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Regular expressions may include Posix character classes, but I don't think that implies that anything that uses a Posix character class is a regex. As another example tr also uses Posix character classes, but is not a regex parser.

– Digital Trauma – 2015-12-14T08:18:08.790

You can shave off a few characters by avoiding useless double quotes, e.g. in the printf format. – Jens – 2015-12-14T14:04:31.553

I've updated the challenge to clarify - the reason regexes were disallowed was to make it so hex patterns could not be used to match uuids – Jojodmo – 2015-12-14T19:40:48.730

4

Jolf, 32 bytes

Try it here!

 eGi'-DN&bH*28=lH.[8,4,4,4,12]S}
 e                               Property "e"very of next object
  Gi'-                           Split i at hyphen
      DN                       } every comparison function
        &                        logical conjugation of next two arguments
         bH*28                   base 16 of H (first arg); is NaN (falsey) if invalid
              =                  equality of next two items
               lH                the length of H (first arg)
                 .            S  the Sth (index) member of the object inbetween
                  [8,4,4,4,12]   array of lengths

Because of an error in my code, this is longer than it should be. :( [8,4,4,4,12] should be the same as {8444*26}, but } is also the closing of a function :P

Conor O'Brien

Posted 2015-12-13T19:40:23.617

Reputation: 36 228

2So it is longer than it has to be because you have ambiguous commands in the language you created? :P – Rɪᴋᴇʀ – 2015-12-14T02:12:45.890

@RikerW Minor semantic mistake. It is fixed now. – Conor O'Brien – 2015-12-14T02:13:24.003

4

MATL, 55 bytes

jttn36=?[9,5,5,5]XsXK)45=?36:Km~)4Y2'A':'F'hm?}F]]]N~1$

I refrained from using the Yb function (strsplit) because it's somewhat similar to regexp(..., 'split'). This only uses indexing and character comparisons.

Example

>> matl
 > jttn36=?[9,5,5,5]XsXK)45=?36:Km~)4Y2'A':'F'hm?}F]]]N~1$
 > 
> This is a test
0

>> matl
 > jttn36=?[9,5,5,5]XsXK)45=?36:Km~)4Y2'A':'F'hm?}F]]]N~1$
 > 
> D293DBB2-0801-4E60-9141-78EAB0E298FF
1

Explanation

jt                     % input string, duplicate
tn36=?                 % if length is 36
  [9,5,5,5]XsXK        % build and copy indices of required '-' positions
  )45=?                % if those entries are indeed '-'
    36:Km~)            % logical index of remaining positions
    4Y2'A':'F'h        % allowed chars in those positions
    m?                 % if all those entries are legal: do nothing
    }                  % else
      F                % false value
    ]                  % end
  ]                    % end
]                      % end
N~                     % true if stack is empty
1$                     % display last result only

Luis Mendo

Posted 2015-12-13T19:40:23.617

Reputation: 87 464

3

CJam, 52 42 bytes

qeu__{A7*)<},\'-/83 3b{)4*}%.{\,=}[1]5*=*=

Try it online. Outputs the original string if true, outputs empty string if false (this is allowed).

Explanation:

qeu__                                      e# Take input, make 2 copies
     {A7*)<},\                             e# Remove invalid characters from first copy
              '-/                          e# Split top of stack on '-
                 83 3b{)4*}%               e# Array of group lengths: [8 4 4 4 12]
                            .{\,=}[1]5*=   e# Compare two arrays, return true if group lengths are correct
                                        *= e# Multiply this value by original string (0 = empty string, 1 = same string)

GamrCorps

Posted 2015-12-13T19:40:23.617

Reputation: 7 058

A7*)< will not remove many invalid characters, like spaces, +, ? ... – Martin Ender – 2015-12-13T22:41:15.437

@MartinBüttner oh shoot... I did not realize that, Ill fix it in a few minutes. – GamrCorps – 2015-12-13T22:42:07.997

3

Julia, 86 bytes

s->(t=split(s,"-");map(length,t)==[8,4,4,4,12]&&all(i->!isnull(tryparse(Int,i,16)),t))

This is an anonymous function that accepts a string and returns a boolean. To call it, give it a name, e.g. f=s->....

Ungolfed:

function f(s::AbstractString)
    # Split the input into an array on dashes
    t = split(s, "-")

    # Ensure the lengths are appropriate
    ok1 = map(length, t) == [8, 4, 4, 4, 12]

    # Ensure each element is a valid hexadecimal number
    ok2 = all(i -> !isnull(tryparse(Int, i, 16)), t)

    return ok1 && ok2
end

Alex A.

Posted 2015-12-13T19:40:23.617

Reputation: 23 761

3

C# 196 bytes

using System.Linq;class P{bool T(string v){var r=v.Length==36;for(var i=0;i<v.Length;i++)r&=new[]{8,13,18,23}.Any(t=>t==i)?v[i]=='-':v[i]>47&&v[i]<58|v[i]>64&&v[i]<71|v[i]>96&&v[i]<103;return r;}}

Ungolfed:

using System.Linq;
class P
{
    public bool T(string v)
    {
        var r = v.Length == 36;
        for (var i = 0; i < v.Length; i++)
            r &= new[] { 8, 13, 18, 23 }.Any(t => t == i) 
                ? v[i] == '-' 
                : v[i] > 47 && v[i] < 58 | v[i] > 64 && v[i] < 71 | v[i] > 96 && v[i] < 103;
        return r;
    }
}

Method T can be invoked with any non-null string and will return true for valid GUID's, false otherwise. This is a constant-time validation; at the cost of three chars you can early-exit the method (change i < v.Length to i < v.Length && r).

Will try to get the bytecount down further later.

I've obviously left out the Guid.ParseExact way because where's the fun in that? Here it is, without much attempt to golf it down further in 86 bytes:

using System;class P{bool T(string v){Guid x;return Guid.TryParseExact(v,"D",out x);}}

Ungolfed:

using System;
class P
{
    bool T(string v)
    {
        Guid x;
        return Guid.TryParseExact(v, "D", out x);
    }
}

RobIII

Posted 2015-12-13T19:40:23.617

Reputation: 397

2

Perl 6,  83  67 bytes

# 83 bytes
{
  (
    my@a=.uc.split('-')
  ).map(*.comb)⊆('0'..'9','A'..'F')
&&
  @a».chars~~(8,4,4,4,12)
}

# 67 bytes
{
  (
    $/=.split('-')
  ).map({:16($_)//|()})==5
&&
  $/».chars~~(8,4,4,4,12)
}

( counts do not include newlines or indents as they are not needed )

usage:

# give it a name
my &code = {...}

say map &code, «
  D293DBB2-0801-4E60-9141-78EAB0E298FF
  0FCE98AC-1326-4C79-8EBC-94908DA8B034
  0fce98ac-1326-4c79-8ebc-94908da8b034
  0FCE98ac-1326-4c79-8EBC-94908da8B034
  00000000-1326-4c79-8EBC-94908da8B034
»;
# (True True True True True)

say map &code, «
  0GCE98AC-1326-4C79-8EBC-94908DA8B034
 '0FCE98AC 1326-4C79-8EBC-94908DA8B034'
  0FCE98AC-13264C79-8EBC-94908DA8B034
  0FCE98AC-13264-C79-8EBC-94908DA8B034
  0FCE98ACD-1326-4C79-8EBC-94908DA8B034
  0FCE98AC-1326-4C79-8EBC-94908DA8B034-
  0FCE98AC-1326-4C79-8EBC-94908DA8B034-123
»;
# (False False False False False False False)

Brad Gilbert b2gills

Posted 2015-12-13T19:40:23.617

Reputation: 12 713

2

Python 2, 99 112 bytes

def f(u):
 try:u=u.split()[0];int(u.replace('-',''),16);print[8,4,4,4,12]==map(len,u.split('-'))
 except:print 0

On a valid input, it prints True. On an invalid input it prints False or 0, depending on why it was invalid. False and 0 are both falsey in Python.

The function has to check 3 things:

  • Every non-hyphen character is a digit or is in ABCDEF
  • There are exactly 4 hyphens
  • There are 8 characters before the first hyphen, 12 after the last, and 4 between any other two

Here's a breakdown to show how it checks for them. It's slightly out of date but I'm hungry so I'll update it later.

def f(u):
    try:
        int(u.replace('-',''),16) # Remove all hyphens from the string and parse what's
                                  # left as a base 16 number. Don't do anything with this
                                  # number, but throw an exception if it can't be done.

        return[8,4,4,4,12]==map(len,u.split('-')) # Split the string at each hyphen and
                                                  # get the length of each resulting
                                                  # string. If the lengths == [8,4,4,4,12],
                                                  # there are the right number of groups
                                                  # with the right lengths, so the string
                                                  # is valid.
    except:
        return 0 # The only way to get here is if the string (minus hyphens) couldn't be
                 # parsed as a base 16 int, so there are non-digit, non-ABCDEF characters
                 # and the string is invalid.

undergroundmonorail

Posted 2015-12-13T19:40:23.617

Reputation: 5 897

I guess you can save 2 bytes if you replace both instances of return with print. (In which case you'll definitely want to be in Python 2, because print works differently in Python 3.) – mathmandan – 2015-12-14T07:45:08.570

1This doesn't work in Python 3, because map now returns a "map object", not a list. – Tim Pederick – 2015-12-14T09:27:06.983

This doesn't work in python 2 (probably 3 too) because the int function allows spaces - 0FCE98ac-1326-4c79-8EBC-94908da8B03 with a trailing space. See comment in deleted Pyth answer here if able.

– Blue – 2015-12-14T19:02:09.300

2

Python 2, 57 bytes

Thank goodness for built-in! - make sure to enclose strings in quotes.

import uuid
try:uuid.UUID(input());print 1
except:print 0

user8777

Posted 2015-12-13T19:40:23.617

Reputation:

5According to the docs you linked to, that would print 1 for input 12345678123456781234567812345678. – Dennis – 2015-12-14T06:00:24.107

if this worked you'd be able to save bytes by doing try:print uuid.UUID(input()) because all that's required is you print a truthy value – undergroundmonorail – 2015-12-14T13:58:51.673

2This program accepts many UUID formats, but the question wants only the 36-character UUID format, with hyphens. – Jacob Krall – 2015-12-15T16:49:52.260

2You could salvage this, coping with the updated rules, by checking if the input string is equal to the uuid converted back to string. Gives you a truthy value right away. – agtoever – 2015-12-16T21:36:26.813

2

Pyth, 39 bytes

&&!+1xzd.xi:zK\-k16ZqxKc+zK1mid36"8dinz

Try it here.

Blue

Posted 2015-12-13T19:40:23.617

Reputation: 26 661

Your "try it here" link is missing the \ character in K\-k, so it fails to run as is. – Alex – 2015-12-15T10:30:21.577

That has been fixed now – Blue – 2015-12-15T11:38:10.433

2

Python 2, 93 89 85 bytes

lambda u:(set(u)<=set("-0123456789abcdefABCDEF"))*map(len,u.split("-"))==[8,4,4,4,12]

The map() call guarantees that the sections are of the right lengths, and the all() tests each character for being either a hyphen or an arbitrary-case hex digit. The generator expression is testing each character by iterating through that entire string, so it's not the most performant method, I'm afraid, but it should satisfy the test cases:

>>> f=lambda u:(set(u)<=set("-0123456789abcdefABCDEF"))*map(len,u.split("-"))==[8,4,4,4,12]
>>> testcases = """\
... D293DBB2-0801-4E60-9141-78EAB0E298FF
... 0FCE98AC-1326-4C79-8EBC-94908DA8B034
... 0fce98ac-1326-4c79-8ebc-94908da8b034
... 0FCE98ac-1326-4c79-8EBC-94908da8B034
... 00000000-0000-0000-0000-000000000000""".splitlines()
>>> failcases = """\
... 0GCE98AC-1326-4C79-8EBC-94908DA8B034
... 0FCE98AC 1326-4C79-8EBC-94908DA8B034
... 0FCE98AC-13264C79-8EBC-94908DA8B034
... 0FCE98AC-13264-C79-8EBC-94908DA8B034
... 0FCE98ACD-1326-4C79-8EBC-94908DA8B034
... 0FCE98AC-1326-4C79-8EBC-94908DA8B034-
... 0FCE98AC-1326-4C79-8EBC-94908DA8B034-123
... 00000000-0000-0000-000 -000000000000
... 00000000-0000-0000- 000-000000000000""".splitlines()
>>> all(f(u) for u in testcases)
True
>>> any(f(u) for u in failcases)
False
>>> 

rsandwick3

Posted 2015-12-13T19:40:23.617

Reputation: 131

Anybody have any idea why the shortest correct Python answer has been downvoted? Not enough explanation? – rsandwick3 – 2015-12-14T23:17:50.163

Jojodmo -- in case there was confusion about this, I did not reject your proposed change -- I had already made an edit because I had missed the A-F characters (I copied from a window where I was testing negative cases), and Community auto-rejected your proposal without me knowing it had even been raised. By the time I knew you'd proposed it, @nimi had already made the correction to the heading.

I very much hope that has nothing to do with the downvote, as that would reflect very poorly on this community. Anyway, I'll assume it's the other thing, and add a bit more explanation. – rsandwick3 – 2015-12-14T23:43:00.847

2You can remove the f= and the spaces around the string in the all block. – FryAmTheEggman – 2015-12-14T23:48:37.620

oh awesome, nice catch -- edited – rsandwick3 – 2015-12-14T23:56:51.300

Strictly speaking, you need to have a print statement in there to get ot to STDOUT – wnnmaw – 2015-12-15T20:24:27.977

1You can save 8 (or 6, you might need to add parenthesis) bytes by converting your all(..) to set inclusion testing: set(u)<=set("-0123456789abcdefABCDEF"). – 301_Moved_Permanently – 2015-12-16T14:50:38.787

oh, and likely a bit more efficient, nice @MathiasEttinger, i'll update it now – rsandwick3 – 2015-12-29T06:17:31.530

@wnnmaw, i missed a couple of the rules at first, too -- in this case:

"If using a function, instead of using STDOUT, the output can be the return value of the function" – rsandwick3 – 2015-12-29T06:20:47.463

2

Common Lisp - 161

(lambda(s &aux(u(remove #\- s)))(and(=(length s)36)(=(length u)32)(every(lambda(p)(char=(char s p)#\-))'(8 13 18 23))(ignore-errors(parse-integer u :radix 16))))

The returned value if true is the hash, as a number, which is a useful result to have.

Ungolfed

(defun uuid-p (string &aux (undashed (remove #\- string)))
  (and
   ;; length of input string must be 36
   (= (length string) 36)

   ;; there are exactly 4 dashes
   (= (length undashed) 32)

   ;; We check that we find dashes where expected
   (every (lambda (position)
            (char= (char string position) #\-))
          '(8 13 18 23))

   ;; Finally, we decode the undashed string as a number in base 16,
   ;; but do not throw an exception if this is not possible.
   (ignore-errors
    (parse-integer undashed :radix 16))))

coredump

Posted 2015-12-13T19:40:23.617

Reputation: 6 292

@Jojodmo Yes, definitely! Thanks – coredump – 2015-12-17T06:43:57.653

2

F# 44 characters

fun s->System.Guid.TryParseExact(s,"D")|>fst

In F#, functions with out parameters can be called by omitting the out parameter; its value at return will be combined with the function's true return value into a tuple.

Here, the tuple is piped to the fst function, which returns its first member, which in this case is the Boolean return value of TryParseExact, indicating the success or failure of the call.

As a check for the correct format, we return true only if the string is 36 characters long.

Before I saw RobIII's C# answer, I had not thought of using TryParseExact, so my answer was to have been three characters longer:

fun s->System.Guid.TryParse s|>fst&&s.Length=36

TryParse(string, Guid) accepts input in the following formats:

00000000000000000000000000000000 
00000000-0000-0000-0000-000000000000 
{00000000-0000-0000-0000-000000000000} 
(00000000-0000-0000-0000-000000000000)
{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

Of these, only the second is 36 characters long.

phoog

Posted 2015-12-13T19:40:23.617

Reputation: 121

1

Java, 345 bytes

interface q{static void main(String[]a){int i=-1;char[]b=a[0].toCharArray();java.io.PrintStream u=System.out;if(b.length>36||b.length<36)u.print(1<0);if(b[8]!='-'||b[13]!='-'||b[18]!='-'||b[23]!='-')u.print(1<0);while(++i<b.length){if(i!=8&&i!=13&&i!=18&&i!=23){if(!((b[i]>='0'&&b[i]<='F')||(b[i]>='a'&&b[i]<='f')))u.print(1<0);}}u.print(1>0);}}

Input is first command line argument. Output is error code(0 means valid UUID, 1 means not valid)

Ungolfed with comments:

interface q {
    static void main(String[] a) {
        int i = -1;                                                             // Index
        char[] b = a[0].toCharArray();                                          // Characters from input
        java.io.PrintStream u = System.out;                                     // STDOUT
        if (b.length > 36||b.length < 36)                                       // If input length is not 36
            u.print(1<0);                                                       // Invalid
        if (b[8]!='-'||b[13]!='-'||b[18]!='-'||b[23]!='-')                      // If hasn't got separators at correct positions
            u.print(1<0);                                                       // Invalid
        while (++i<b.length) {                                                  // Iterate over all characters
            if (i!=8 && i!=13 & i!=18 && i!=23) {                               // If not at separator indexes
                if ( !( (b[i]>='0'&&b[i]<='F') || (b[i]>='a'&&b[i]<='f') ))     // If incorrect hexadecimal number
                    u.print(1<0);                                               // Invalid
            }
        }
        u.print(1>0);                                                           // Valid
    }
}

EDIT: Didn't notice the STDOUT part. Oops, fixed now.

cookie

Posted 2015-12-13T19:40:23.617

Reputation: 271

Nice! You can replace if(b.length>36||b.length<36) with simply if(b.length!=36). Also, because you can print truthy values, you could simply print 0 instead of 1<0, and 1 instead of 1>0.

– Jojodmo – 2017-03-14T01:44:33.997

@Jojodmo Based on the votes, a truthy value is in the form of if(truthy_value){ doSomethingOnYes(); } else{ doSomethingOnFalse(); } So in Java a boolean is a truthy value, but 1 or 0 isn't. Only when the OP of a challenge says something like: "Your output can be true/false, 0/1, empty/non-empty; as long as you specify what you use." then you can indeed use 0 and 1 instead of true/false as truthy/falsey value.

– Kevin Cruijssen – 2017-04-14T13:15:31.880

1As for golfing tips for cookie: @Jojodmo is indeed right with replacing it for if(b.length!=36); || can be | on multiple places, as well as && to &; if(...!='-') can be if(...!=45); int i=-1; ... while(++i<b.length){ can be replaced with for(int i=-1;++i<b.length;){; 'F' can be 70 ('f' could be 102, but that doesn't matter since it's the same byte-count). I like how you've used java.io.PrintStream u=System.out; btw, I should remember that one! So thanks. – Kevin Cruijssen – 2017-04-14T13:19:24.253

1

PHP, 109 Bytes

prints 1 for true and 0 for false

for($t=($l=strlen($a=$argn))==36;$i<$l;$i++)$t*=$i>7&$i<24&!($i%5-3)?$a[$i]=="-":ctype_xdigit($a[$i]);echo$t;

$i>7&$i<24&!($i%5-3) is 5 Bytes shorter then in_array($i,[8,13,18,23])

112 Bytes

echo array_filter(str_split($argn),function($i){return!ctype_xdigit($i);})==[8=>"-",13=>"-",18=>"-",23=>"-"]?:0;

113 Bytes

echo array_diff(str_split(strtolower($argn)),array_map(dechex,range(0,15)))==[8=>"-",13=>"-",18=>"-",23=>"-"]?:0;

Jörg Hülsermann

Posted 2015-12-13T19:40:23.617

Reputation: 13 026

1

Swift 3, 50 bytes

Pass in a string s

import Foundation
print(UUID(uuidString:s) != nil)

Matt

Posted 2015-12-13T19:40:23.617

Reputation: 289

1

SAS, 171 144 141

data;infile stdin;file stdout;input a$9b$14c$19d$24;e=(a!!b!!c!!d='----')*length(_infile_)=36*(1-missing(put(input(compress(_infile_,,'adk'),$hex32.),$hex32.)));put e;run;

Actually uses stdin and stdout - one of the lesser-known features of this particular language. Works for the examples given so far, but possibly not in all cases. Can probably be improved upon.

Better approach - one character at a time:

data;infile stdin;file stdout;do i=1 to 37;input@i c$1.@;a+ifn(i in(9,14,19,24),c='-',n(input(c,hex.))-36*(i>36&c^=''));end;b=a=36;put b;run;

Golfed another 6 characters off the central expression!

Ungolfed:

data;
infile stdin;
file stdout;
do i=1 to 37;
input@i c$1.@;
a+ifn(i in(9,14,19,24),c='-',n(input(c,hex.))-36*(i>36&c^=''));
end;
b=a=36;
put b;
run;

This generates quite a few warnings and notes in the log, but it doesn't print them to stdout or stderr, so I think this is fair game.

user3490

Posted 2015-12-13T19:40:23.617

Reputation: 809

1

C, 391 bytes

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define F printf("0")
#define T printf("1")
#define E return 0
main(){char s[99],*t;int k=1,l,i;scanf("%99[^\n]",s);if(s[strlen(s)-1]=='-'){F;E;}t=strtok(s,"-");while(t!=NULL){for(i=0,l=0;t[i]!=0;i++,l++){if(!isxdigit(t[i])){F;E;}}if((k==1&&l!=8)||((k>1&&k<5)&&l!=4)||(k==5&&l!=12)){F;E;}k++;t=strtok(NULL,"-");}if(k==6){T;E;};F;}

user2064000

Posted 2015-12-13T19:40:23.617

Reputation: 270

1

MATLAB, 126 bytes

function f(a)
b='-';if length(a)==36&&a(9)==b&&a(13)==b&&a(17)==b&&a(21)==b;a(a==b)=[];if any(isnan(hex2dec(a)));0;end;1;end;0

costrom

Posted 2015-12-13T19:40:23.617

Reputation: 478

1

Python 3, 134 bytes

def a(i):
 try:l=[1+int(k,16)and(len(k)==c)for k,c in zip(i.split("-"),[8,4,4,4,12])];return(len(l)==5)&(0 not in l)
 except:return 0

int(k,16) tries to cast k to a base-16 int. On a character other than 0-9a-fA-F- it fails, in which case we return 0, which is falsy. Add 1 to that int and we get a guaranteed truthy value - we've stripped away all hyphens with str.split() so we can't get the value -1 and all non-0 ints are truthy.

Pjoelj

Posted 2015-12-13T19:40:23.617

Reputation: 11

1

C function, 102

A rule change disallowed my previous c scanf()-based answer, so here's another c answer using isxdigit() which I think should be allowed to compete:

i;f(char *s){for(i=8;i<24;i+=5)s[i]=s[i]-45?1:s[i]+3;for(i=0;isxdigit(s[i]);i++);return i==36&&!s[i];}

Try it online.

  • Check for - characters (ASCII 45) at the relevant positions - if so, replace them with 0s (ASCII 48 (=45+3))
  • Walk the string checking each char with isxdigit()
  • Return TRUE if string length is 36 and final character is NUL.

Digital Trauma

Posted 2015-12-13T19:40:23.617

Reputation: 64 644

1

Batch, 148 139 + 2 = 150 141 bytes

@set/pu=
@for %%d in (1 2 3 4 5 6 7 8 9 A B C D E F)do @set u=!u:%%d=0!
@if -!u!==-00000000-0000-0000-0000-000000000000 exit/b0
@exit/b1

Added 2 bytes because you need to use the /v switch to CMD.EXE.

Exits with ERRORLEVEL 0 on success, 1 on failure.

Edit: Saved some bytes mainly because := is case insensitive but there were other tweaks too.

Neil

Posted 2015-12-13T19:40:23.617

Reputation: 95 035

0

Java, 172 bytes 168 bytes (Thanks Wheat Wizard)

Kinda cheaty since I used java.util.UUID, but here goes:

import java.util.UUID;class ValidUUID{public static void main(String[] a){try{UUID.fromString(a[0]);System.out.println(1);}catch(Exception e){System.out.println(0);}}}

Ungolfed version:

import java.util.UUID;

class ValidUUID {

    public static void main(String[] a) {
        try {
            UUID.fromString(a[0]);
            System.out.println(1);
        } catch(Exception e) {System.out.println(0);}
    }
}

ryxn

Posted 2015-12-13T19:40:23.617

Reputation: 1

Welcome to the site! I think you can remove the space between try and {. – Post Rock Garf Hunter – 2017-03-11T18:41:51.857

@WheatWizard thank you :D also noticed I can remove the " around 0 and 1 – ryxn – 2017-03-11T19:21:26.933

2You should be able to remove the space between String[] and a. Also, you should be able to replace println with print. – clismique – 2017-03-12T07:25:28.437

1Class name can be 1 character. You can use java.util.UUID.fromString instead of importing. – Poke – 2017-04-05T13:41:33.440

0

AWK, 98 bytes

BEGIN{FS=""}{for(j=4;k<NF;){h+=(j+=5)<25?$j=="-":0
s+=strtonum("0x"$++k 1)>0}$0=h+s==36&&NF==36}1

Simply splits the line at every character and checks if each character is a hex digit and that there are hyphens at the appropriate places. strtonum converts invalid characters to 0. Making the comparison between 0 and m (and arbitrarily chosen invalid character) require additional steps. Luckily 01 is a valid hex number, but m1 is not.

Initially I wrote two for loops, but I saved 1 byte by squeezing them together. :)

NOTE: GAWK can read input as hex numbers, but this requires a very long command line option.

Robert Benson

Posted 2015-12-13T19:40:23.617

Reputation: 1 339

0

Python 2, 41 bytes

from uuid import*;lambda a:UUID(a).hex==a

Python has a builtin module for handling UUIDs.

pppery

Posted 2015-12-13T19:40:23.617

Reputation: 3 987