Building a regex with UUID in between for unit testing?

Ryan :

I have a unique file name generator and struggling with the REGEX pattern. I want to assert within my unit test that the output string is valid:

    private static final String FILE_NAME_DELIMITER = "-";
    private static final Supplier<String> getRandomUUID = () ->
            UUID.randomUUID().toString().replace(FILE_NAME_DELIMITER, "");

This is how the UUID is generated.

This is the output string filePrefix-yyyyMMdd-UUID.csv.gz

The regex I am looking for is any_length_alpha_numeric_prefix-yyyyMMdd-UUID.extensionType.gz

  • any_length_alpha_numeric_prefix = any letters or numbers of variable length
  • yyyyMMdd = this date format should exist in this location
  • UUID = the UUID generated from the above code
  • extensionType.gz = needs to be .csv.gz
Meow Cat 2012 :

Do you want to match to test if the file name is formatted as expected, or to extract date and UUID from the file name?

If you simply want to test the file name, the regex would be:

.*-[0-9]{8}-[0-9a-f]{32}\.[^.]+\.gz

(New to regex? Be aware that those backslashes are requried by regex itself but not java compiler. With nowaday IDE's, additional backslashes should be automatically added when pasting into your code. If not, replace any \'s with \\'s. )

Explain:

  1. Match any_length_alpha_numeric_prefix and a delimiter: .* for arbitrary content and - for itself. A . in regex matches anything.

  2. yyyyMMdd and a delimiter: Here we use [0-9]{8}- for simplicity to match any 8 digits instead of strictly valid dates. For strict date validation there're many on the Internet.

  3. For UUID and a dot: An UUID is 32 characters (0~f hex digits) without delimiters. So [0-9a-f]{32} would be sufficient. Then \., the dot is escaped so now it matches itself.

  4. extensionType: We assume it to be an arbitrary sequence without dots. So we use [^.]+ to match one or more non-dot characters. You can also hardcode it to csv instead, if all files are known to be csv files.

  5. The extension .gz: Use \.gz.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=295988&siteId=1