I want to parse a string in order to get the value of an attribute.For example i have the following String: calling_number=+385317KFWVM, call_direction=I, conversation_duration=29,
Each attribute and its value is separated by commas.I have declared the name and the type of each attribute in a class named MessageBean.
public static void main(String[] args){
try {
MessageBean attributes_bean = MessageBean.parse("calling_number=+385317KFWVM, call_direction=I, conversation_duration=29,");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class MessageBean {
private String calling_number;
private String call_direction;
private int conversation_duration;
public static MessageBean parse(String line) throws UnsupportedEncodingException {
MessageBean bean = new MessageBean();
line = URLDecoder.decode(line, "utf-8");
bean.setCalling_number(MessageBean.getParameter(line, "calling_number"));
bean.setCall_direction(MessageBean.getParameter(line, "call_direction"));
bean.setConversation_duration(Integer.parseInt(MessageBean.getParameter(line, "conversation_duration")));
return bean;
}
private static String getParameter(String line, String name) {
String value = "";
Pattern p = Pattern.compile(name + "=([^,]*),");
Matcher m = p.matcher(line);
if (m.find()) {
value = m.group(1);
}
return value;
}
}
I expect my result to be +385317KFWVM, I, 29 .
Instead i get 385317KFWVM, I, 29 which means that i miss the + sign.I understand that there is a problem with my regular expression.I have tried everything like \\+ but i still don't get the right results.Any help?
当您运行URLDecoder.decode(line,“ utf-8”)时,将从字符串中删除+。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.