This question is old and it isn't particularly clear: you don't explain how you've tried to use the regex. I'll attempt to answer it anyway.
First off, there are no "special" variables $1
or $2
like in Perl or Ruby. With that out of the way, it becomes a simple matter of how to use the various procedures.
For example, with string-match
, you simply receive a list of matches:
#;1> (use regex regex-literals)
#;2> (string-match #/b(a)(r)/ "bar")
("bar" "a" "r")
So, to refer to the Nth submatch you'd use (list-ref the-match N)
(where 0 equals the complete matched string).
With string-substitute
and when using back references within the regex, you'd use "\\1"
(you have to use two backslashes to escape the backslash in string context):
#;1> (use regex regex-literal)
#;2> (string-substitute #/f(o)(\1)/ "lala\\2xyz" "foo")
"lalaoxyz"
This works in general, but there's an undocumented feature (or perhaps a bug) that if you use a backslash in front of an escape sequence in the replacement, it will be escaped. See this bugreport on how that works, and how to use irregex instead of the regex egg to aovid this.