0

我有一个大文本文件,正在寻找一个能够删除 8-10 个字符之间的所有 id 编号并将它们保存为逗号分隔的正则表达式。

文件的一行如下:

{"follow_request_sent": null, "profile_use_background_image": true, "id": 340671834, "description": "This is The Official Fans Page Of One Direction! We will update you with the latest news 24/7! Follow  https://t.co/ZJLh8usZ80 and follow @itsdirectlieber", "verified": false, "profile_image_url_https": "https://si0.twimg.com/profile_images/3057650709/cc214d87e8b65324677f3a99bdff3bd4_normal.jpeg", "profile_sidebar_fill_color": "E8EAEB", "profile_text_color": "949494", "followers_count": 353767, "protected": false, "location": "Brazil", "default_profile_image": false, "id_str": "340671834", "status": {"favorited": false, "contributors": null, "truncated": false, "text": "Ill make a test now, bye.", "created_at": "Fri Apr 12 11:27:01 +0000 2013", "retweeted": false, "in_reply_to_status_id": null, "coordinates": null, "id": 322672198861090816, "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", "in_reply_to_status_id_str": null, "place": null, "id_str": "322672198861090816", "in_reply_to_screen_name": null, "retweet_count": 10, "geo": null, "in_reply_to_user_id_str": null, "in_reply_to_user_id": null}, "utc_offset": -10800, "statuses_count": 22301, "profile_background_color": "FFFFFF", "friends_count": 60311, "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/837595304/7352cbbc52911b16816807c7fc39824d.png", "profile_link_color": "E80C38", "profile_image_url": "http://a0.twimg.com/profile_images/3057650709/cc214d87e8b65324677f3a99bdff3bd4_normal.jpeg", "notifications": null, "geo_enabled": false, "profile_banner_url": "https://si0.twimg.com/profile_banners/340671834/1365451996", "profile_background_image_url": "http://a0.twimg.com/profile_background_images/837595304/7352cbbc52911b16816807c7fc39824d.png", "name": "One Direction", "lang": "en", "following": null, "profile_background_tile": false, "favourites_count": 301, "screen_name": "1DFAMlLY", "url": "http://www.facebook.com/1DFAMlLY", "created_at": "Sat Jul 23 02:36:18 +0000 2011", "contributors_enabled": false, "time_zone": "Brasilia", "profile_sidebar_border_color": "FFFFFF", "default_profile": false, "is_translator": false, "listed_count": 1317}

我正在尝试删除 id 并将其保存为单独的逗号分隔字符串

非常感谢任何帮助。

4

1 回答 1

1

您可以使用{N,M}运算符来定义正则表达式中的字符范围。例如,\\d{8,10}可用于查找 8-10 个字符之间的数字。

为了查找所有 id,您使用以下正则表达式:

String input = "your string here";
Pattern pattern = Pattern.compile("\"id\":\\s*(\\d{8,10})\\s*[,}]");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
    System.out.println(matcher.group(1));
}

但是,您应该考虑使用 JSON 解析器。它更适合这项工作,并且将来可能会为您省去一些麻烦。

于 2013-04-13T16:36:19.407 回答