2

I have a string field. I need to pass UUID string or digits number to that field. So I want to validate this passing value using regex.

sample :

stringField = "1af6e22e-1d7e-4dab-a31c-38e0b88de807";
stringField = "123654";

For UUID I can use,

"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"

For digits I can use

"\\d+"

Is there any way to use above 2 pattern in single regex

3
  • Why don't you just try and parse with UUID.fromString() and if that fails, do new BigInteger()? More simple and no need for regexes Commented Jul 5, 2013 at 7:13
  • @fge On that note, found this interesting issue. Commented Jul 5, 2013 at 7:19
  • This has nothing to do with the fact that you are seeking to parse UUIDs Commented Jul 5, 2013 at 7:22

4 Answers 4

3

Yes..you can use |(OR) between those two regex..

[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}|\\d+
                                                            ^
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think this will work. You will need parentheses to make groups. This is just making the last group either a group of 12 alphanumerics or more than one number. I could be completely wrong, though.
@Ryaminal i guess op only wants to validate the input..he doesn't want groups..op should clarify on this though
1

try:

"(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})|(?:\\d+)"

Comments

1

You can group regular expressions with () and use | to allow alternatives.

So this will work:

(([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12})|(\\d+)

Note that I've adjusted your UUID regular expression a little to allow for upper case letters.

5 Comments

@Ryaminal - just trying to be multiplatform.
Why all the parens? All that needs capturing is the result. .group() will be enough.
@fge indeed you are correct but I like to keep my regular expressions as portable as possible. Feel free to edit my answer; including an alternative specific to Java.
I don't see where portability enters into account here? Can you elaborate? All regex engines have the capacity to return the matched text, as far as I know...
I'm with @fge; I don't know of any regex flavor that requires you to use groups the way you're doing here.
1

How are you applying the regex? If you use the matches(), all you have to do is OR them together as @Anirudh said:

return myString.matches(
    "[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}|\\d+");

This works because matches() acts as if the regex were enclosed in a non-capturing group and anchored at both ends, like so:

"^(?:[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}|\\d+)$"

If you use Matcher's find() method, you have to add the group and the anchors yourself. That's because find() returns a positive result if any substring of the string matches the regex. For example, "xyz123<>&&" would match because the "123" matches the "\\d+" in your regex.

But I recommend you add the explicit group and anchors anyway, no matter what method you use. In fact, you probably want to add the inline modifier for case-insensitivity:

"(?i)^(?:[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}|\\d+)$"

This way, anyone who looks at the regex will be able to tell exactly what it's meant to do. They won't have to notice that you're using the matches() method and remember that matches() automatically anchors the match. (This will be especially helpful for people who learned regexes in a non-Java context. Almost every other regex flavor in the world uses the find() semantics by default, and has no equivalent for Java's matches(); that's what anchors are for.)


In case you're wondering, the group is necessary because alternation (the | operator) has the lowest precedence of all the regex constructs. This regex would match a string that starts with something that looks like a UUID or ends with one or more digits.

 "^[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}|\\d+$"  // WRONG

1 Comment

By forcing the start and end anchors Alan is preventing accidental matches if there are substrings which look like your uuid or digits. e.g. this is not a valid match-12345678 would return true without the anchors. +1 for well thoughout

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.