-1

An example unique identifier for example is:

"67a9854c-f63c-ef4a-0908-001fa4ff6512"

What is the regex to match this string?

  • 2
    What have you tried? Show some code or the regex you've come up with – Lino Jun 22 '18 at 18:27
  • Can you post more examples? With only one example, we can trivially write a regex that matches that exact string, which is probably not what you want - we can't help you unless we know the pattern you want to match. – R Balasubramanian Jun 22 '18 at 18:28
  • See [this Q&A](https://stackoverflow.com/a/24387746/335858) – Sergey Kalinichenko Jun 22 '18 at 18:29
  • 1
    (onr of) The RE to match that String is `"67a9854c-f63c-ef4a-0908-001fa4ff6512"`, but I doubt that is what you really want to know – user85421 Jun 22 '18 at 18:30
  • See the [JavaDoc](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html), which you can [try online](https://www.freeformatter.com/java-regex-tester.html). – M. le Rutte Jun 22 '18 at 18:32

1 Answers1

0

Making some simple assumptions about the format of your identifier, I came up with "((?:\w){8}-(?:\w){4}-(?:\w){4}-(?:\w){4}-(?:\w){12})"

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "((?:\\w){8}-(?:\\w){4}-(?:\\w){4}-(?:\\w){4}-(?:\\w){12})";
final String string = "67a9854c-f63c-ef4a-0908-001fa4ff6512";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

Code generated using the excellent Regex101. I'd definitely recommend playing around with that next time, as it's very user-friendly.

A. Dysart
  • 16
  • 4