R
rico.fabrini
Hi Everyone,
I've got the following code:
======================================================================
private static final Pattern rgxUrlsInHTML = Pattern.compile("(http:\\/
\\/([\\w.]+\\/?)\\S*)");
public static String emailBodyToHtml(String body)
{
Matcher matcher = rgxUrlsInHTML.matcher(body);
int start = 0;
ArrayList<String> matches = new ArrayList<String>();
while(matcher.find())
{
matches.add(matcher.group());
}
for(String match : matches)
{
int index = body.indexOf(match);
if(index>-1)
body = body.replaceFirst(match, "<a href=\"" + match+ "\">"+match
+"</a>");
}
}
======================================================================
1) I might have missed it, but I couldn't find a method that would
just return a collection of all matches.
Accordingly, the API feels rather low-level. Probably sufficient,
but still low-level.
2) My actual query here:
body = body.replaceFirst(match, "<a href=\"" + match+ "\">"+match
+"</a>");
seems to work as I would wish only for the first member match of the
matches collection.
Even though the if block is executed, the 2nd URL isn't substituted.
Is there something glaringly obvious that's eluding me here?
Also, comments for improvement from people familiar with the Regular
Expressions API are welcome.
Thanks.
Rico.
I've got the following code:
======================================================================
private static final Pattern rgxUrlsInHTML = Pattern.compile("(http:\\/
\\/([\\w.]+\\/?)\\S*)");
public static String emailBodyToHtml(String body)
{
Matcher matcher = rgxUrlsInHTML.matcher(body);
int start = 0;
ArrayList<String> matches = new ArrayList<String>();
while(matcher.find())
{
matches.add(matcher.group());
}
for(String match : matches)
{
int index = body.indexOf(match);
if(index>-1)
body = body.replaceFirst(match, "<a href=\"" + match+ "\">"+match
+"</a>");
}
}
======================================================================
1) I might have missed it, but I couldn't find a method that would
just return a collection of all matches.
Accordingly, the API feels rather low-level. Probably sufficient,
but still low-level.
2) My actual query here:
body = body.replaceFirst(match, "<a href=\"" + match+ "\">"+match
+"</a>");
seems to work as I would wish only for the first member match of the
matches collection.
Even though the if block is executed, the 2nd URL isn't substituted.
Is there something glaringly obvious that's eluding me here?
Also, comments for improvement from people familiar with the Regular
Expressions API are welcome.
Thanks.
Rico.