clojure regex named groups

I have a problem with re-find in clojure. Actually I'm doing

(re-find #"-(?<foo>\d+)-(?<bar>\d+)-(?<toto>\d+)-\w{1,4}$" 
"" )

My result is fine:

["-9-31289-824-gt" "9" "31289" "824"]

But I would prefer to have a hash looking like:

{:foo "9" :bar "31289" :toto "824"}

I have understood that java.util.regex.Matcher/group is doing something like that but I haven't been able to use it correctly. Thanks for your help


  • JDK didn't support named capture groups until JDK 7.

    Here's announcement on oracle blog


    This convenient feature has been missed in Java RegEx for years, now it finally got itself in JDK7 b50.

    Since clojure supports JDK >= 6 you're out of luck if you're looking for something native (clojure uses java regex Patterns and Matchers behind the scenes).

    You can always use external libraries, like named-re. That one gives you exactly what you need.


    (require 'named-re.core)
    (re-find #"-(?<foo>\d+)-(?<bar>\d+)-(?<toto>\d+)-\w{1,4}$" 
         "" )

    will return

    {:toto "824", :bar "31289", :foo "9", :0 "-9-31289-824-gu"}