[erlang-questions] Problems with re:replace/4

Paul Burt paul.burt@REDACTED
Fri Jul 22 09:39:22 CEST 2011


Hi,

I'm a newcomer to Erlang and am interested in writing some helper
functions to parse and manipulate responses from the Twitter API for
use in a web application.

What I would like to do is replace "@mention" and "#hashtag" with HTML
markup as follows:

@mention --> <a href="http://twitter.com/mention">@mention</a>
#hashtag --> <a href="http://search.twitter.com/search?tag=hashtag">#hashtag</a>

In other words transform Twitter @mentions and #hashtags into clickable links.

I have the basic structure of a function (tweetify/1) to do this
below. The ???? represent where I'm running into trouble:

tweetify(Input) ->
    Replacements = [
        {"@(?<mention>\\w+)", "<a href=\"http://twitter.com/????\">&</a>"},
        {"#(?<hashtag>\\w+)", "<a
href=\"http://search.twitter.com/search?tag=????\">&</a>"}
    ],
    lists:foldl(fun({RE, Replacement}, Tweet) ->
                      re:replace(Tweet, RE, Replacement, [global,
{return, binary}])
                  end, Input, Replacements).

Whilst I understand that the token "&" will insert _everything_
matched by the regular expression (i.e. @mention and #hashtag
respectively), how do I use the named tags in the replacement string?
In other words, how do I get hold of \k<mention> and \k<hashtag> in
the PCRE idiom and use them in the replacement string?

Thanks in advance for any assistance/pointers.

-Paul



More information about the erlang-questions mailing list