Skip to content

Instantly share code, notes, and snippets.

@awesome
Last active September 5, 2022 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesome/020dcdc66b177ca4b5b8619c575ecfdd to your computer and use it in GitHub Desktop.
Save awesome/020dcdc66b177ca4b5b8619c575ecfdd to your computer and use it in GitHub Desktop.
UUID pure regexp and dash/hyphen is optional; return only full uuid matched
# https://ihateregex.io/expr/uuid/
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/
# https://rubular.com/r/sXJ27X08h7tKZ2 refactor by soawesomeman
/([0-9a-fA-F]{8}-?(?:[0-9a-fA-F]{4}-?){2}[0-9a-fA-F]{4}-?[0-9a-fA-F]{12})/
# https://rubular.com/r/lIO6HeHPlHcTl3 further refactor by soawesomeman
# see: https://stackoverflow.com/questions/15205896/matching-any-character-except-an-underscore-using-regex#15206584
/((?:(?!_)[\w\d]){8}-?(?:(?:(?!_)[\w\d]){4}-?){3}-?(?:(?!_)[\w\d]){12})/
# https://rubular.com/r/PFrxaj9h9GZRPX further refactor by soawesomeman
# scope to word boundary with `\b`
/\b((?:(?!_)[\w\d]){8}-?(?:(?:(?!_)[\w\d]){4}-?){3}-?(?:(?!_)[\w\d]){12})\b/
# https://rubular.com/r/mjHJuRzi050p41 further refactor by soawesomeman
# digits included in `\w`; removed `\d`
\b((?:(?!_)\w){8}-?(?:(?:(?!_)\w){4}-?){3}-?(?:(?!_)\w){12})\b
@awesome
Copy link
Author

awesome commented Jul 6, 2022

while we're on the subject…

# https://stackoverflow.com/questions/4692437/regex-with-named-capture-groups-getting-all-matches-in-ruby#comment55272306_13817639
string.scan(REGEXP_GOES_HERE).inject([]) {|array,match| array << $~; array}
string.scan(REGEXP_GOES_HERE).each_with_object([]) {|match,array| array << $~}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment