I'm using this method to parse out plain text URLs in some HTML and make them links
private String fixLinks(String body) {
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
body = body.replaceAll(regex, "<a href=\"1ドル\">1ドル</a>");
Log.d(TAG, body);
return body;
}
No URLs are replaced in the HTML however. The regular expression seems to be matching URLs in other regular expression testers. What's going on?
asked Nov 29, 2011 at 20:34
SeanPONeil
3,9204 gold badges31 silver badges43 bronze badges
1 Answer 1
The ^ anchor means the regex can only match at the start of the string. Try removing it.
Also, it looks like you mean 0ドル rather than 1ドル, since you want the entire match and not the first capture group, which is (https?|ftp|file).
In summary, the following works for me:
private String fixLinks(String body) {
String regex = "(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
body = body.replaceAll(regex, "<a href=\"0ドル\">0ドル</a>");
Log.d(TAG, body);
return body;
}
answered Nov 29, 2011 at 20:42
NPE
503k114 gold badges970 silver badges1k bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
SeanPONeil
Worked like a charm. You just saved me from jumping off a bridge.
lang-java
\s*after the^to allow for whitespace.