Convert plain text to HTML text in Java

前端 未结 6 644
-上瘾入骨i
-上瘾入骨i 2020-12-05 10:53

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 an

6条回答
  •  感情败类
    2020-12-05 11:50

    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("$1");
    

    And Here are the input and output -

    Input text is variable plain:

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

    Output :

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

提交回复
热议问题