2013-04-13 2 views
0

큰 텍스트 파일이 있고 8-10 자 사이의 모든 ID 번호를 제거하고 쉼표로 구분하여 저장할 수있는 정규식을 찾고 있습니다. 내가 ID를 제거하고 별도의 쉼표로 구분 된 문자열정규식 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} 

다음과 같이 파일의

한 행입니다.

+1

입력 텍스트 및 예상 출력에 대한 추가 정보가 필요합니다. 또한 어떻게 해결하려했는지 보여줄 수 있습니까? – Pshemo

+4

대신 JSON 구문 분석기를 사용하지 않는 이유는 무엇입니까? – Tiago

+0

RegEx는 모든 문자열 조작 문제의 만병 통치약이 아닙니다. 이 경우 가장 좋은 방법은 JSON 파서 라이브러리를 사용하는 것입니다. –

답변

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 파서를 사용하는 것이 좋습니다. 그것은이 작업에 훨씬 더 적합하며 미래에 두통을 덜어줍니다.

관련 문제