Search code examples
androidjsonregexhrefreplaceall

Adding a href tag to a long string from a JSON


I've been stuck on a problem where I have a json object with a value in it titled message, which will have lots of string and some will even contain links in the format below (like how this site has different formats for their bold, italics, etc)

[[www.randomwebsite.com]] gives www.randomwebsite.com

or

[[www.randomwebsite.com random]] gives random

The regex that I have for the top one is:

\[\[.[^\]]*\]\]

and I'm doing a .replaceAll to try and add the href tags to it, but it isn't working as I feel I've done it wrong. My code for that is

String htmlHyperlinkMessage = htmlStrikeMessage.replaceAll(Constants.HYPERLINK_REGEX, "$1<a href=\"$2\"></a>");

but this gives me an array out of bounds exception and I'm just stuck on this, can anyone give any suggestions?

It's greatly appreciated, I have looked around on the forum but I couldn't find anything related to my example as the format for different hyperlinks has confused me.

EDIT

Here's my stacktrace logcat

java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at java.util.regex.Matcher.group(Matcher.java:579)
at java.util.regex.Matcher.appendEvaluated(Matcher.java:138)
at java.util.regex.Matcher.appendReplacement(Matcher.java:111)
at java.util.regex.Matcher.replaceAll(Matcher.java:319)
at java.lang.String.replaceAll(String.java:1600)
at com.myapp.android.model.PostItem.getMessage(PostItem.java:98)

Solution

  • When you're replacing something with a group (like $1, $2), you need to capture that group using parentheses. You're getting an out-of-bounds exception because $1 tries to find a group you made, but your regex \[\[.[^\]]*\]\] contains no groups.

    If you want to capture the contents of [[]] as a group, you can add parentheses: \[\[(.[^\]]*)\]\]

    For a further explanation, read the "3.4. Grouping and back reference" part of this article: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

    And the replacement "$1<a href=\"$2\"></a>" doesn't seem right either. It should be something like "<a href=\"$1\">$1</a>"

    By the way, I don't think the . character in your regex is needed. It does make sure the square brackets aren't empty, but you can achieve the same effect more clearly by replacing the * with + like this:

    \[\[([^\]]+)\]\]