Question

I need regex which loosely matches UUIDs and numbers. I expect my filename to be formatted like:

results_SOMETHING.csv

This something ideally should be numbers (count of how many time a script is run) or a UUID.

This regex is encompasses a huge set of filenames:

 ^results_?.*.csv$

and this one:

 ^results_?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}.csv$

matches only UUIDs. I want a regex whose range is somewhere in between. Mostly I don't want matches like result__123.csv.

Was it helpful?

Solution

Note: This doesn't directly answer the OP question, but given the title, it will appear in searches.

Here's a proper regex to match a uuid based on this format without the hex character constraint:

(\w{8}(-\w{4}){3}-\w{12}?)

If you want it to match only hex characters, use:

/([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}?)/i

(Note the / delimiters used in Javascript and the /i flag to denote case-insensitivity; depending on your language, you may need to write this differently, but you definitely want to handle both lower and upper case letters).

If you're prepending results_ and appending .csv to it, that would look like:

^results_([a-z\d]{8}(-[a-z\d]{4}){3}-[a-z\d]{12}?).csv$

OTHER TIPS

-----EDITED / UPDATED-----

Based on the comments you left, there are some other patterns you want to match (this was not clear to me from the question). This makes it a little more challenging - to summarize my current understanding:

results.csv                                         - match (NEW)
results_1A.csv                                      - match (NEW)
results_ABC.csv                                     - ? no match (I assume)
result__123.csv                                     - no match
results_123.csv                                     - match
Results_123.cvs                                     - ? no match
results_0a0b0c0d-884f-0099-aa95-1234567890ab.csv    - match

You will find the following modification works according to the above "specification":

results(?:_(?:[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}|(?=.*[0-9])[A-Z0-9]+))?\.csv

Breaking it down:

results               matches characters "results" literally
(?:_ ….)?             non-capturing group, repeated zero or one time:
                      "this is either there, or there is nothing"
[0-9a-f]{8}-          exactly 8 characters from the group [0-9a-f]
                      followed by hyphen "-"
(?:[0-9a-f]{4}-){3}   ditto but group of 4, and repeated three times
[0-9a-f]{12}          ditto, but group of 12
|                     OR...
(?=.*[0-9]+)          at least one number following this
[A-Z0-9]+             at least one capital letter or number
\.csv                 the literal string ".csv" (the '.' has to be escaped)

demonstration on regex101.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top