私は文字列を解析したいattribute.For例の値を取得するために、私は以下の文字列があります。 calling_number = + 385317KFWVM、call_direction = I、conversation_duration = 29、を解析JavaのString特殊文字エラー
各属性とその値はカンマで区切ります。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;
}
}
は、私は私の結果は+ 385317KFWVM、私は、29ことを期待しています。
代わりに、私は私の定期的なexpression.Iに問題があることを理解し\ +のようなすべてのものを試してみましたが、私はまだ取得しない+ sign.Iを欠場することを意味し385317KFWVM、I、29を取得助けてください。
これを[mcve]に減らしてください。正規表現についてはちょうど*のように見えるので、 'Pattern'を作成し、ハードコードされた文字列と照合して結果を報告する' main'メソッドが必要です。それはあなたを助けるのがずっと簡単で、将来の読者にとってもより価値があります。 –
それが問題を示していない場合は、渡される文字列の値に本当に+が含まれていることを確認してください。 L.Sからの答えは非常に説得力があります。これは、Stack Overflowでpostingする前に診断(ロギング、デバッガ)*でチェックするべきものです。 https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ –
私はStackOverflowコミュニティに新しくなっています。私はあなたにアドバイスをします。Jon Skeet。たくさんありがとうございます。 –