Java 删除除<;之外的所有HTML标记;br>;从一个文本?
大家好 我有一个java字符串,我想 1-从中删除除新行标记Java 删除除<;之外的所有HTML标记;br>;从一个文本?,java,regex,Java,Regex,大家好 我有一个java字符串,我想 1-从中删除除新行标记和之外的所有html标记,如果有文本,则将文本保留在标记内。 2-解析后,文本结果彼此连接,如:text1和text2,文本之间没有空格分隔,我也想这样做 以下是我正在做的: String html = "<div dir=\"ltr\">hello my friend<span>ECHO</span><br>how are you ?<br><br><div
和
之外的所有html标记,如果有文本,则将文本保留在标记内。
2-解析后,文本结果彼此连接,如:text1和text2,文本之间没有空格分隔,我也想这样做
以下是我正在做的:
String html = "<div dir=\"ltr\">hello my friend<span>ECHO</span><br>how are you ?<br><br><div class=\"gmail_quote\">On Mon, Feb 14, 2011 at 10:45 AM, My Friend <span dir=\"ltr\"><<a href=\"mailto:notifications@mydomain.com\">notifications@mydomain.com</a>></span> wrote:<br> "
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\"> ";
String parsedText = html.replaceAll("\\<.*?\\>", "");
System.out.println(parsedText);
期望输出:
hello my friend ECHO <br> how are you ? <br> <br> On Mon, Feb 14, 2011 at 10:45 AM, My Friend &`lt;notifications@mydomain.com> wrote:`
你好,我的朋友ECHO
你好吗<2011年2月14日星期一上午10:45,我的朋友<;notifications@mydomain.com写道:`
我会的
- 用换行符或其他特殊字符替换所有
- 删除所有标签
- 将特殊字符替换为
- 用换行符或其他特殊字符替换所有
- 删除所有标签
- 将特殊字符替换为
- 我会的
final String html =
"<div dir=\"ltr\">hello my friend<span>ECHO</span><br>how are you ?" +
"<br><br><div class=\"gmail_quote\">On Mon, Feb 14, 2011 at 10:45 AM," +
" My Friend <span dir=\"ltr\"><<a href=\"mailto:notifications@mydo" +
"main.com\">notifications@mydomain.com</a>></span> wrote:<br><bloc" +
"kquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; bord" +
"er-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\"> ";
final Pattern tagPattern = Pattern.compile("<([^\\s>/]+).*?>");
final Matcher matcher = tagPattern.matcher(html);
final StringBuffer sb = new StringBuffer(html.length());
while(matcher.find()){
matcher
.appendReplacement(sb, matcher.group(1).equalsIgnoreCase("br")
? matcher.group()
: " ");
}
matcher.appendTail(sb);
final String parsedText = sb.toString();
System.out.println(parsedText);
最终字符串html=
“你好,我的朋友回声
你好吗?”+
“
2011年2月14日星期一上午10:45,
”+
“我的朋友写道:
”;
最终模式tagPattern=Pattern.compile(“/]+).*?>”;
final Matcher Matcher=tagPattern.Matcher(html);
final StringBuffer sb=新的StringBuffer(html.length());
while(matcher.find()){
匹配器
.附录替换(sb,匹配器组(1).等信号情况(“br”)
?matcher.group()
: " ");
}
(某人);
最后一个字符串parsedText=sb.toString();
System.out.println(解析文本);
输出:
hello my friendECHO<br>how are you ?<br><br>On Mon, Feb 14, 2011 at 10:45 AM,
My Friend <notifications@mydomain.com> wrote:<br>
你好,我的朋友回声
你好吗?
2011年2月14日星期一上午10:45,
我的朋友notifications@mydomain.com写道:
但我希望你知道这一点。不要用正则表达式解析HTML/XML 您可以这样做:
final String html =
"<div dir=\"ltr\">hello my friend<span>ECHO</span><br>how are you ?" +
"<br><br><div class=\"gmail_quote\">On Mon, Feb 14, 2011 at 10:45 AM," +
" My Friend <span dir=\"ltr\"><<a href=\"mailto:notifications@mydo" +
"main.com\">notifications@mydomain.com</a>></span> wrote:<br><bloc" +
"kquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; bord" +
"er-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\"> ";
final Pattern tagPattern = Pattern.compile("<([^\\s>/]+).*?>");
final Matcher matcher = tagPattern.matcher(html);
final StringBuffer sb = new StringBuffer(html.length());
while(matcher.find()){
matcher
.appendReplacement(sb, matcher.group(1).equalsIgnoreCase("br")
? matcher.group()
: " ");
}
matcher.appendTail(sb);
final String parsedText = sb.toString();
System.out.println(parsedText);
最终字符串html=
“你好,我的朋友回声
你好吗?”+
“
2011年2月14日星期一上午10:45,
”+
“我的朋友写道:
”;
最终模式tagPattern=Pattern.compile(“/]+).*?>”;
final Matcher Matcher=tagPattern.Matcher(html);
final StringBuffer sb=新的StringBuffer(html.length());
while(matcher.find()){
匹配器
.附录替换(sb,匹配器组(1).等信号情况(“br”)
?matcher.group()
: " ");
}
(某人);
最后一个字符串parsedText=sb.toString();
System.out.println(解析文本);
输出:
hello my friendECHO<br>how are you ?<br><br>On Mon, Feb 14, 2011 at 10:45 AM,
My Friend <notifications@mydomain.com> wrote:<br>
你好,我的朋友回声
你好吗?
2011年2月14日星期一上午10:45,
我的朋友notifications@mydomain.com写道:
但我希望你知道这一点。不要用正则表达式解析HTML/XML 看看这个问题
看看这个问题
可能重复:不,我不想删除所有html标记,因为代码实际上就是这么做的,我想删除除新行标记以外的所有html标记。可能重复:不,我不想删除所有html标记,因为代码实际上就是这么做的,我想删除除新行标记以外的所有html标记。非常感谢上面的示例,但是您的意思是我应该使用像Jsoup这样的html解析器吗?我的意思是,对于像这样的简单示例,正则表达式可能是可以的,但如果它变得更复杂,是的,Jsoup可能是一个好主意。@Sean:“但我希望你知道,如果你打电话的话,克图鲁正在打电话。“有趣的回答:)那么文本格式hello my friendECHO应该在them@James不幸的是,这不是我自己的想法:非常感谢上面的例子,但是你的意思是我应该用像Jsoup这样的html解析器来做吗?我的意思是,对于像这样一个简单的例子,正则表达式可能是可以的,但是如果它变得更复杂一点,是的,JSoup可能是个好主意。@Sean:“但我希望你知道,如果你这么做,Cthulhu正在打电话。”有趣的回答:)那么文本格式hello my friendECHO之间应该有一个空格them@James不幸的是,这不是我自己的想法: