2012-03-01 3 views
1

String으로 읽으려면 다음 코드를 사용하십시오.REGEX : 큰 문자열 구문 분석

public String convertStreamToString(InputStream is) { 
    try { 
     return new Scanner(is).useDelimiter("\\A").next(); 
    } catch (NoSuchElementException e) { 
     return ""; 
    } 
} 

출력이 매우 커서 String이 표시됩니다. 어떻게하면 두 개의 별도 문자열로 Free blocksBlock count 값을 얻으려고 파싱 할 수 있습니까?

03-01 09:28:25.772: I/System.out(8849): tune2fs 1.41.10 (10-Feb-2009) 
03-01 09:28:25.772: I/System.out(8849): Filesystem volume name: <none> 
03-01 09:28:25.772: I/System.out(8849): Last mounted on:   <not available> 
03-01 09:28:25.772: I/System.out(8849): Filesystem UUID:   c6a5907b-71b3-4686-9ed1-8dd932c6359b 
03-01 09:28:25.772: I/System.out(8849): Filesystem magic number: 0xEF53 
03-01 09:28:25.772: I/System.out(8849): Filesystem revision #: 1 (dynamic) 
03-01 09:28:25.772: I/System.out(8849): Filesystem features:  has_journal ext_attr dir_index filetype sparse_super 
03-01 09:28:25.772: I/System.out(8849): Filesystem flags:   unsigned_directory_hash 
03-01 09:28:25.772: I/System.out(8849): Default mount options: (none) 
03-01 09:28:25.772: I/System.out(8849): Filesystem state:   clean 
03-01 09:28:25.772: I/System.out(8849): Errors behavior:   Continue 
03-01 09:28:25.772: I/System.out(8849): Filesystem OS type:  Linux 
03-01 09:28:25.772: I/System.out(8849): Inode count:    24480 
03-01 09:28:25.772: I/System.out(8849): Block count:    97656 
03-01 09:28:25.772: I/System.out(8849): Reserved block count:  4882 
03-01 09:28:25.772: I/System.out(8849): Free blocks:    90433 
03-01 09:28:25.772: I/System.out(8849): Free inodes:    24469 
03-01 09:28:25.772: I/System.out(8849): First block:    1 
03-01 09:28:25.772: I/System.out(8849): Block size:    1024 
03-01 09:28:25.772: I/System.out(8849): Fragment size:   1024 
03-01 09:28:25.772: I/System.out(8849): Blocks per group:   8192 
03-01 09:28:25.772: I/System.out(8849): Fragments per group:  8192 
03-01 09:28:25.772: I/System.out(8849): Inodes per group:   2040 
03-01 09:28:25.772: I/System.out(8849): Inode blocks per group: 255 
03-01 09:28:25.772: I/System.out(8849): Filesystem created:  Thu Mar 1 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Last mount time:   n/a 
03-01 09:28:25.772: I/System.out(8849): Last write time:   Thu Mar 1 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Mount count:    0 
03-01 09:28:25.772: I/System.out(8849): Maximum mount count:  35 
03-01 09:28:25.772: I/System.out(8849): Last checked:    Thu Mar 1 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Check interval:   15552000 (6 months) 
03-01 09:28:25.772: I/System.out(8849): Next check after:   Tue Aug 28 04:01:37 2012 
03-01 09:28:25.772: I/System.out(8849): Reserved blocks uid:  0 (user root) 
03-01 09:28:25.772: I/System.out(8849): Reserved blocks gid:  0 (group root) 
03-01 09:28:25.772: I/System.out(8849): First inode:    11 
03-01 09:28:25.772: I/System.out(8849): Inode size:   128 
03-01 09:28:25.772: I/System.out(8849): Journal inode:   8 
03-01 09:28:25.772: I/System.out(8849): Default directory hash: half_md4 
03-01 09:28:25.772: I/System.out(8849): Directory Hash Seed:  fb1c85fc-7de0-431e-be07-aa42bee66e7d 
03-01 09:28:25.772: I/System.out(8849): Journal backup:   inode blocks 
03-01 09:28:25.819: I/System.out(8849): length = 1512 
03-01 09:28:25.819: I/System.out(8849): 2 

답변

3

[편집] 죄송합니다 당신은 스캐너와 함께 무엇을하고 있는지 몰랐 : 당신에게 무료 블록과 블록 수를 단지 값을 줄 것이다

Scanner scanner = new Scanner(is).useDelimiter("\\n"); 
    scanner.findWithinHorizon(Pattern.compile("Block\\scount:\\s*(\\d+)"), 0); 
    String blockCount = scanner.match().group(1); 
    scanner.findWithinHorizon(Pattern.compile("Free\\sblocks:\\s*(\\d+)"), 0); 
    String freeBlocks = scanner.match().group(1); 

. 예를 들어, freeBlocks은 "90433"이고 blockCount은 "97656"과 같습니다.

+0

'03-01 10 : 07 : 49.436 : W/System.err (9432) : java.lang.IllegalStateException : 지금까지 성공한 일치가 없습니다 '. : –

+0

여전히 null을 반환합니다. –

+0

지금은 일치하지만 전체 줄을 반환합니다. 문제는 순서와 같습니다 .BlockCount가 먼저 배치되어야합니다. –

0
Scanner sc = new Scanner(is); 
while (sc.hasNextLine()) { 
    String line = sc.nextLine(); 
    // If line contains information 
     // Parse Line 
    // Else 
     // continue 
} 

당신이 찾고있는 정보가 라인에 포함되어 있는지 확인하려고하는 논리를 남깁니다.