Linkify Android 转换问题

发布于 2024-11-16 13:59:10 字数 734 浏览 4 评论 0原文

尝试做一些相当简单的事情。

像这样获取文本

用户名:这是我正在发表的评论

,它位于单个 TextView 中。我想让用户名成为一个链接。我决定最简单的事情就是用“$@”之类的东西包围用户名,这样它就变成了

“$@用户名:$@这是我正在发表的评论

这样我就可以在 Linkify 中使用以下正则表达式

Pattern userName = Pattern.compile(".*\\$@(.+)\\$@.*");

并将其设为链接。但是,显然我需要删除分隔符,因此以下是

title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {

    @Override
    public String transformUrl(Matcher match, String url) {
       return match.group(1);
    }
};
Linkify.addLinks(title, userName, "content://user=", null,     transformer);

一些 代码然而,由于原因,整个文本变成了一个巨大的链接,并且文本根本没有被转换。

Trying to do something fairly simple.

Taking text like this

User Name: This is a comment I am making

It is in a single TextView. I want to make the User Name a link. I decided that the easiest thing would be to surround the User Name with something like "$@" so it becomes

"$@User Name:$@ This is a comment I am making

That way I can use the following regular expression

Pattern userName = Pattern.compile(".*\\$@(.+)\\$@.*");

with Linkify and make it a link. However, clearly I need to remove the delimiters, so the following is the code

title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {

    @Override
    public String transformUrl(Matcher match, String url) {
       return match.group(1);
    }
};
Linkify.addLinks(title, userName, "content://user=", null,     transformer);

For some reason however, the whole text becomes one giant link, and the text isn't being transformed at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合久必婚 2024-11-23 13:59:10

事实上,事实证明这非常简单。我最终没有使用疯狂的“$@”来分隔用户名,而是坚持使用

用户名:这是我正在发表的评论

所以我最终使用了以下模式

Pattern userName = Pattern.compile("(.+:)");

非常简单,代码变得只是

title.setText(titleText);
Linkify.addLinks(title, GlobalUtil.userName, "user://" + userId + "/");

感谢 nil 的原始建议。我确实匹配了整个字符串,而不仅仅是链接的用户名。

It actually did turned out to be pretty easy. I ended up not using the crazy "$@" to delimit the username, instead sticking with just

User Name: This is a comment I am making

so I ended up using the following pattern

Pattern userName = Pattern.compile("(.+:)");

Very simple, and the code becomes just

title.setText(titleText);
Linkify.addLinks(title, GlobalUtil.userName, "user://" + userId + "/");

Thank you to nil for the original suggestion. I was indeed matching the whole string instead of just the userName which is the link.

暮凉 2024-11-23 13:59:10

我最好的猜测是您使用的正则表达式是问题所在,您告诉它基本上挑选出整个字符串(如果匹配),包括您要查找的内容之前和之后的所有内容。因此,TransformFilter 可能会传递整个匹配的字符串。据我所知,transformUrl 期望您返回 URL,因此整个字符串都链接到第一个匹配组。

因此,考虑到这一点,将正则表达式更改为类似 "\\$@(.+?)\\$@" (添加 < code>? 在组中使匹配非贪婪),以避免匹配整个字符串并仅挑选出您想要 URL-ize 的位(由于缺乏更好的术语,再加上 -ize言语听起来很酷)。

My best guess is the regex you're using is the problem, where you're telling it to basically pick out the entire string if it matches, including everything before and after what you're looking for. So, the TransformFilter is probably being passed the entire matched string. transformUrl as far as I can tell expects you to return the URL, so the entire string is linked to the first match group.

So, with that in mind, it's probably in your best interest to change the regex to something along the lines of "\\$@(.+?)\\$@" (with an added ? in the group to make the match non-greedy) so as to avoid matching the entire string and just picking out the bit you want to URL-ize (for lack of a better term, plus adding -ize to words sounds cool).

如果没有 2024-11-23 13:59:10

为什么不将分隔符放在模式内进行更改?

Pattern userName = Pattern.compile(".*(\\$@.+\\$@).*");

然后更改转换过滤器以在更改为 URL 时删除开始和结束模式...

Why not put the delimiters inside the pattern to change ?

Pattern userName = Pattern.compile(".*(\\$@.+\\$@).*");

Then change the transform filter to remove the start and end patterns when changing into the URL...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文