2012-09-11 86 views
2

我想读取文本文件作为C++阅读如何读取和解析此文本文件的内容?

我这里是从文本文件

(item(name 256) (desc 520)(Index 1)(Image "Wea001") (specialty (aspeed 700))) 
(item (name 257) (desc 520)   (Index 2) (Image "Wea002")(specialty(Attack 16 24))) 

我希望输出像

name : 256 
Desc : 520 
Index : 1 
Image : Wea001 
Specialty > aspeed : 700 

Name : 257 
Desc : 520 
Index : 2 
Image : Wea002 
Speciality > Attack : 16 24 

这可能吗?

我想:

preg_match_all('/name\s+(.*?)\)\+\(desc\s+(.*?)\)\+\(Index\s+(.*?)\)/', $text, $matches, PREG_SET_ORDER); 

foreach ($matches as $match) { 
    list (, $name, $desc, $index) = $match; 
    echo 'name : '.$name.' <br> Desc : '.$desc.'<br> Index : '.$index.''; 
    } 

但它并没有给我正确的输出,我想。

谢谢

+3

圣牛是一个讨厌的格式。它来自哪里?你可以改变它吗? – DaveRandom

+0

Lisp攻击!这么多括号! – Tchoupi

回答

2
<?php 
    $txt = '(item(name 256) (desc 520)(Index 1)(Image "Wea001") (specialty (aspeed 700))(item (name 257) (desc 520)   (Index 2) (Image "Wea002")(specialty(Attack 16 24)))'; 

    preg_match_all('/name\s+(?P<name>\w+).*desc\s+(?P<desc>\d+).*Index\s+(?P<index>\d+).*Image\s+(?P<img>.*)\).*specialty\s*\((?P<speciality>.*)\)\)\)/', $txt, $matches); 
    foreach($matches['name'] AS $id => $name){ 
     echo 'name : '.$name.' <br> Desc : '.$matches['desc'][$id].'<br> Index : '.$matches['index'][$id].'<br> speciality : '.$matches['speciality'][$id].''; 
} 

假设你有总是相似的数据格式

+0

非常感谢,它工作正常 – DragoN