ykaganovich ykaganovich - 1 year ago 40
Java Question

Is there a java library that validates URNs?

What's the easiest way to validate that a string is a valid URN?

Edit Using URI is not a correct solution! URIs are allowed to have all kinds of things that URNs can't, like


Answer Source

If you only need to validate it, you can use a regular expression. The following will match only RFC2141 compliant URNs:

import java.util.regex.Pattern;

public class UrnTest {
    public final static Pattern URN_PATTERN = Pattern.compile(

    public static void main(String[] args) throws Exception {
        for(String urn : args) {
            boolean isUrn = URN_PATTERN.matcher(urn).matches();
            System.out.println(urn+"  :  "+(isUrn ? "valid" : "not valid"));