I'm using a WebView in an Android app written in Kotlin to display some rich text content that is given via an API response. The data is given as raw text mixed with some HTML components (links) which is displayed in the app.
The goal of writing this Regex filter is to capture raw links not wrapped in <a> tags, and reformat them so that they are displayed richly in the web view.
My current attempts at solving this problem include using replace() with an inline function to capture the link and replace it with an <a> tag.
I have an overridden function which takes the data to be loaded into the WebView and adds some style data as well as removing any <iframe> tags. Those two components work, however my replacement using the pattern to match URLs does not have any effect.
Given a list:
http://example.com
https://example.com
I expect an output:
<a href="http://example.com">auto link</a>
<a href="https://example.com">auto link</a>
Yet my pattern yields unchanged input.
I am following a URL matching pattern found here: https://mathiasbynens.be/demo/url-regex
val pattern = "@(https?|ftp)://(-\\.)?([^\\s/?\\.#-]+\\.?)+(/[^\\s]*)?$@iS\n".toRegex()
data.replace(pattern) {
"<a href=\"${it.groupValues[1]}\">auto link</a>"
}
Log.i("TEST", data)
This function fails to replace the data with the right link whenever I log it, even though I know that the pattern matches the links I am feeding it.

data? If it is an immutable string, assign a value to it.data = data.replace(pattern) { "<a href=\"${it.groupValues[1]}\">auto link</a>" }