html超链接如何转化为字符串的问题
假设现在我想分析一个锚标签<a href="http://tech.sina.com.cn/n/2008-05-22/08132210131.shtml" target="_blank">低预算购本攻略</a>
但当我尝试将其转化为字符串时会出现错误Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ),我已经用\恢复它字面意思的啊,我的字符串如下:String str = "<a href=\"http:\/\/tech\.sina\.com.cn\/n\/2008-05-22\/08132210131\.shtml\" target=\"_blank\">低预算购本攻略<\/a>";
[解决办法]
String s = "<a href=\"http://tech.sina.com.cn/n/2008-05-22/08132210131.shtml\" target=\"_blank\">";
[解决办法]
String str = "<a href='http://tech.sina.com.cn/n/2008-05-22/08132210131.shtml' target='_blank'>";
[解决办法]
你可能需要这段代码
private String htmlFilter( String line ) { if( line == null || line.equals("") ) { return ""; } // replace ampersands with HTML escape sequence for ampersand; line = replace(line, "&", "&"); // replace \" sequences with HTML escape sequences; line = replace(line, "\\\"", "\""); // replace the \\ with HTML escape sequences. fixes a problem when // backslashes preceed quotes. line = replace(line, "\\\\", "\\" ); // replace less-than signs which might be confused // by HTML as tag angle-brackets; line = replace(line, "<", "<"); // replace greater-than signs which might be confused // by HTML as tag angle-brackets; line = replace(line, ">", ">"); return multiLineCommentFilter(line); }
[解决办法]
./在正则表达式中要转义,但在java字符串中是不需要转义的
String str = "\\\"\\.\\/";
写正则表达式要这样才行