如何读取并解析该文本文件的内容?

2023-11-29

我想以 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.'';
  }

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

谢谢


<?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].'';
}

假设您始终具有相似的数据格式

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何读取并解析该文本文件的内容? 的相关文章

随机推荐