在Java中将纯文本转换为HTML文本 [英] Convert plain text to HTML text in Java

查看:169
本文介绍了在Java中将纯文本转换为HTML文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有java程序,它将从服务器接收纯文本。纯文本可能包含URL。 Java库中是否有任何类将纯文本转换为HTML文本?还是其他任何图书馆?如果没有那么解决方案是什么?

I have java program, which will receive plain text from server. The plain text may contain URLs. Is there any Class in Java library to convert plain text to HTML text? Or any other library? If there are not then what is the solution?

推荐答案

我找到了一个使用模式匹配的解决方案。这是我的代码 -

I found a solution using pattern matching. Here is my code -

String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»""‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(plain);
plain = matcher.replaceAll("<a href=\"$1\">$1</a>");

以下是输入和输出 -

And Here are the input and output -

输入文本是变量普通

some text and then the URL http://www.google.com and then some other text.

输出:

some text and then the URL <a href="http://www.google.com">http://www.google.com</a> and then some other text.

这篇关于在Java中将纯文本转换为HTML文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆