RE: URI Templates: { ^ prefix ^ variable [] separator | default }

This syntax can be parsed with a single regular expression:

\{(\^)?(?:([^\^]*?)\^?)(|[a-zA-Z][a-zA-Z\._~-]*+)(?:\[\]([^\|\}]*+))?(?:\|([^\}]*))?\}

Groups 1-5 correspond to ^ (encoding), <prefix>, <variable>, <separator> and <default> respectively.

Or in Java (which extra \’s to escape \’s):

        public static final Pattern REGEX = Pattern.compile(
            "\\{" +                             //   {
                "(\\^)?" +                      // 1  ^
                "(?:([^\\^]*?)\\^?)" +          // 2  prefix ^
                "(|[a-zA-Z][a-zA-Z\\._~-]*+)" + // 3  variable
                "(?:\\[\\]([^\\|\\}]*+))?" +    // 4  [] separator
                "(?:\\|([^\\}]*))?" +           // 5  | default
            "\\}");                             //   }

Substituting { ^ prefix ^ variable [] separator | default } gives:
1. if variable is undefined: <default> (or an empty string)
2. if variable is a single value: <prefix><variable>
3. if variable is a list: <prefix><variable[0]><separator><variable[1]>…

Received on Friday, 26 October 2007 05:53:08 UTC