用单行和多行解析文件 [英] Parsing a file with single and multi-lines of data

查看:183
本文介绍了用单行和多行解析文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件转储,我需要转换为分隔文件。该文件包含一系列记录(缺少更好的单词),格式如下:

$ p $ User:abc123
日期:7/3/12
主题:foo是bar
项目:123456
问题:富文本多行文本
分辨率:foo un-barred in多行文本

用户:abc123
日期:7/3/12
主题:foo是bar
项目:234567
问题:foo在多行文本
中可以包括< newline>和
扩展为多行文本
分辨率:foo在多行文本中不加限制

...

现在,使用Java,我使用StringBuffer逐行读取这个文件,根据一系列 if(inputLine.toLowerCase()。startsWith(user:))逻辑输出一个最终的分隔线到文本文件。

然而, Problem 和 Resolution 字段是自由格式, may 可以多行。我试图做一些会创建两个字符串的东西:追加后面的所有行问题:并以结尾> 并追加所有以分辨率:结尾表单:



我alerady查看此链接此链接,这表明 StringBuilder 可能是一个适当的方法来做到这一点...但是,我不太清楚如何构建逻辑。

编辑:
由于我是逐行阅读,所以我很难如何编码

 < pseudocode> 
如果以Problem开头的行提取Problem后面的字符else
如果PRIOR行以problem开头,当前行不以resolution开头,则将字符添加到prior行

< /伪代码>

但是,如果有第三行问题...?我只是无法可视化如何使它工作。



任何想法或替代方法实现我想要的结果?

解决方案

如果我正确理解你的问题,那么沿着这些行应该工作:
$ b $ pre $ StringBuilder problemDesc = new (...)
if(inputLine.toLowerCase()。startsWith(problem:)){
problemDesc.append(inputLine);
while(!inputLine.toLowerCase() .startsWith(resolution:){
//将下一行读入inputline;
problemDesc.append(inputline);
}
//在这里处理问题描述和inputLine现在与
一致//分辨率在其中重复用于检索分辨率值的相同逻辑

code $ pre

I have a text file dump that I need to convert to a delimited file. The file contains a series of "records" (for lack of a better word) formatted like this:

User: abc123 
Date: 7/3/12
Subject: the foo is bar
Project: 123456
Problem: foo bar in multiple lines of text
Resolution: foo un-barred in multiple lines of text

User: abc123 
Date: 7/3/12
Subject: the foo is bar
Project: 234567
Problem: foo bar in multiple lines of text
          which may include <newline> and 
          extend to multiple lines of text
Resolution: foo un-barred in multiple lines of text

...

Right now, with Java, I'm using StringBuffer to read this file line-by-line, parsing the lines to individual fields based on a series of if(inputLine.toLowerCase().startsWith("user:")) logic to output a final delimited line to a text file.

However, the fields Problem and Resolution are free form and may be multi-line. I'm trying to do something that would create two Strings: append all lines following Problem: and ending at Resolution: and append all lines starting after Resolution: and ending at Form:.

I've alerady viewed this link and this link, which suggest that StringBuilder might be an appropriate way to do this...however, I'm not quite sure how to construct the logic.

EDIT: Since I'm reading line-by-line, I'm having a hard time wrapping my head around how to code

<pseudocode>
If the line starts with "Problem" extract the charactes after "Problem" else
if the PRIOR line starts with "problem" and the current line doesnt start with "resolution" then append characters in line to prior line
etc.
</pseudocode>

but then, if there's a third line of "Problem...? I just can't visualize how to make it work.

Any ideas or alternate methods of achieving my desired results?

解决方案

Hi if I understand your problem correctly then something along these lines should work:

    StringBuilder problemDesc = new String....;
    if(inputLine.toLowerCase().startsWith("problem:")){
       problemDesc.append(inputLine);
       while(!inputLine.toLowerCase().startsWith("resolution:"){
           //read next line into inputline;
           problemDesc.append(inputline);
       }
       //deal with problem description here and inputLine now has the line with
       //Resolution in it Repeat same logic for retrieving the resolution value
    }

这篇关于用单行和多行解析文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆