[英]Inserting XML words to MySQL using Java Pattern and Matcher
I want to keep track of word usage in a group chat using MySQL database. 我想在使用MySQL数据库的群聊中跟踪单词的用法。 Currently messages passed into the insertWords method is an XML string. 当前,传递到insertWords方法中的消息是XML字符串。 The XML string can have special characters such as '
XML字符串可以具有特殊字符,例如'
and "
和"
. 。 Is there a better way than using String.replace to convert XML formatted strings to normal messages? 是否有比使用String.replace将XML格式的字符串转换为普通消息更好的方法?
If my message is: I'm bad, but they aren't that "good"
如果我的消息是: I'm bad, but they aren't that "good"
How can I convert it to: I'm bad, but they aren't that "good"
我如何将其转换为: I'm bad, but they aren't that "good"
My code will insert apos 2 times and quot 2 times. 我的代码将插入apos 2次和quot 2次。 How do I fix this? 我该如何解决?
Pattern p = Pattern.compile("[\\w']+");
PreparedStatement insertWordStmt = connection.prepareStatement("INSERT INTO word (word, count) VALUES (?, 1) " +
"ON DUPLICATE KEY UPDATE count=count+1");
public void insertWords(String msg) {
msg = msg.toLowerCase();
try {
Matcher m = p.matcher(msg);
while ( m.find() ) {
String word = msg.substring(m.start(), m.end());
insertWordStmt.setString(1, word);
insertWordStmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
to add double quote matching, try 添加双引号匹配,请尝试
Pattern p = Pattern.compile("[\\w'\"]+");
EDIT after comments 评论后编辑
msg = msg.toLowerCase().replace("'","'").replace(""e;","\"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.