REGEX: парсинг большой строки

1

Я читаю InputStream для String используя следующий код.

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

Однако я получаю очень большую String качестве вывода. Как я могу разобрать его, чтобы просто получить значения для Free blocks и Block 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

2 ответа

3
Лучший ответ

[Edited] Извините, не понял, что вы делали со своим сканером:

    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: No successful match so far . :(
  • 0
    Все еще возвращает ноль
Показать ещё 3 комментария
0
Scanner sc = new Scanner(is);
while (sc.hasNextLine()) {
    String line = sc.nextLine();
    // If line contains information
        // Parse Line
    // Else
        // continue
}

Я оставлю логику, пытаясь определить, содержит ли строка информацию, которую вы ищете.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню