How to use regex to replace URLs with an HTML link in Qt? -
how use qstring::replace
detect urls in string , replace them html link, so...
[...].replace(qregexp("???"), "<a href=\"\\1\">\\1</a>")
what should argument qregexp
be? end of url should denoted occurrence of whitespace character (such space, \r or \n) or end of string.
the regex should simple: http://, https://, ftp://, etc, followed 1 or more non-whitespace characters, should converted link.
edit: solution used...
[...].replace(qregexp("((?:https?|ftp)://\\s+)"), "<a href=\"\\1\">\\1</a>")
i think (?:https?|ftp)://\\s+
you.
don't forget potentially match invalid urls, that's ok purposes. (a regex matches syntactically valid urls quite complicated construct , not worth effort.)
Comments
Post a Comment