0

Hi I have this working code to detect a valid UUID pattern.

String pattern = "\\b[a-f0-9]{8}\\b-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-\\b[a-f0-9]{12}\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(fileName);

This correctly detects strings like:

0101a8ef-db10-405a-a1d2-6bebdeb17625

I would like to add two exact strings on each side of this pattern like so:

FOLDER/0101a8ef-db10-405a-a1d2-6bebdeb17625.txt

Here is the code I am trying, but is not working:

String pattern = "FOLDER/\\b//[a-f0-9]{8}\\b-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-\\b[a-f0-9]{12}\\b.txt";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(uri.toString());
5
  • What's the point of \b? Commented Jun 15, 2021 at 19:17
  • 1
    Why not like FOLDER/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\\.txt regex101.com/r/CJjLUP/1 Commented Jun 15, 2021 at 19:18
  • @shmosel Not sure, it was part of the code example that I got the first pattern from. Commented Jun 15, 2021 at 19:18
  • 2
    @Thefourthbird this works, thanks! You should add an answer to this post for future reference. Commented Jun 15, 2021 at 19:22
  • UUID.fromString already does this check. Commented Jun 15, 2021 at 22:10

1 Answer 1

2

There is a // in the pattern that is not in the example string. Besides that, you can omit the word boundaries in between a character a-f0-9 and a - because it is implicit.

Note to escape the dot to match it literally, and you can add the word boundaries at the start and at the end to prevent partial matches.

You could update the pattern to

\\bFOLDER/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\\.txt\\b

See a regex demo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.