java - 正则表达式删除 ID 号

标签 java regex numbers

我有一个很大的文本文件,正在寻找一个正则表达式,能够删除 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 并将其另存为单独的逗号分隔字符串

非常感谢任何帮助。

最佳答案

您可以使用{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 解析器。它更适合这项工作,并且可能会在将来为您省去一些麻烦。

关于java - 正则表达式删除 ID 号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989842/

相关文章:

java - 如果一个文件中有多个类,Java 是否需要一个公共(public)类?

java - 递归期间的全局变量

c# - 正则表达式在 C# 中获取字符串的 2 个部分

php - 在php中显示SQL查询结果

java - Spring 3.1.2 MVC @ExceptionHandler 和 @ResponseBody

java - RMI:如果只有一个 JVM,则远程方法的参数是否必须实现可序列化?

mysql - 通过正则表达式匹配搜索 MySQL 数据库(反向)

regex - 带管道的 Perl 正则表达式

java - 如何显示多位小数? ( java )

input - LABVIEW 中的舍入