从java中的复杂字符串中提取特定单词/字符串

时间:2012-03-25 19:03:20

标签: java

我有一个长字符串,格式将与此字符串相同,但字符串中的消息可能会有所不同。所以我想知道,如何从java中的这个复杂字符串中提取此特定消息字符串

    charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&post_form_id=71c3b72f4049d394140cedf32d39f525&fb_dtsg=AQBY3vp-&feedback_params=%7B%22actor%22%3A%22176851262376586%22%2C%22target_fbid%22%3A%22283157315079313%22%2C%22target_profile_id%22%3A%22176851262376586%22%2C%22type_id%22%3A%227%22%2C%22source%22%3A%222%22%2C%22assoc_obj_id%22%3A%22%22%2C%22source_app_id%22%3A%220%22%2C%22extra_story_params%22%3A%7B%22photo_viewer_version%22%3A%222%22%7D%2C%22content_timestamp%22%3A%221327693760%22%2C%22check_hash%22%3A%22129f5441c4cb4266%22%7D&translate_on_load=&add_comment_text_text=I%20didn't%20got%20any%20msg%20in%20my%20mailbox%20%3A(&add_comment_text=I%20didn't%20got%20any%20msg%20in%20m%20inbox%20%3A(&comment_replace=optimistic_comment_2931473608_0&comment=1&lsd&post_form_id_source=AsyncRequest&__user=18802987&phstamp=165895111811245853

我想以下面的格式提取这个特定的字符串

    I didn't got any msg in my mailbox

1 个答案:

答案 0 :(得分:4)

这是一个正则表达式解决方案:

String input = "charset_test=%E2%8...3A(&add_comment_text=I%20didn't%20got%20any"
             + "%20msg%20in%20m%20inbox%20%3A(&comment_replace=optim"
             + "istic_comment_2931473608_0&comment=1&lsd&post_form_id_source="
             + "AsyncRequest&__user=18802987&phstamp=165895111811245853";

Pattern p = Pattern.compile("add_comment_text_text=(.*?)[&$]");
Matcher m = p.matcher(input);

if (m.find()) {
    String value = URLDecoder.decode(m.group(1), "UTF-8");
    System.out.println(value);
}

<强>输出:

I didn't got any msg in my mailbox :(