使用grep查找3行文本的模式,然后转换为XML

时间:2015-05-06 14:26:07

标签: regex grep bbedit

我正在构建一个BBEdit Textfactory(grep / regex),它将一组重复的3行转换为XML块,但由于我的理解有限,我遇到了麻烦正则表达式。

每个条目有3行 - 名称 \ 1 ,日期 \ 4 ,段落 \ 6 - 然后模式重演。

唯一的外卡是在军队服役的人:然后他/她的名字后面有一个标签:set boxwidth 0.3 plot for [i=1:3] "A.txt" using (i-0.15):i notitle,\ for [i=1:3] "B.txt" using (i+0.15):i notitle \ 2

现在我只能让它部分工作。旗帜标签似乎总是追加到 \ 1 位置的人名末尾,甚至虽然它有自己的组括号。

需要隔离标志,以便稍后可以将其放在XML中。

任何帮助将不胜感激! (还有正则表达式构建建议,如果它真的很糟糕。)

以下是原始的错误模式,仅供参考:

<FLAG>

以下是替换模式:

^([A-z].*)(<[A-Z]{4}>)?(\r?)([A-z].*)(\r?)([A-z].*)(\r?){1}

以下是要处理的文本块的示例:

<item>\r
<category>Obituary</category>\r
<title>\1</title>\r
<description>\r
<em>\4</em><br />\r
\6\r
</description>\r
<pubDate><DATE></pubDate>\r
\2
</item>\r

1 个答案:

答案 0 :(得分:1)

这似乎完成了工作(至少在BBEdit 11中)!

^([A-Za-z. ]+[^<])(<[A-Z]{4}>)?(\r?\n?)([A-z].*)(\r?\n?)([A-z].*)(\r?\n?){1}

输出正确

<item>
<category>Obituary</category>
<title>Wom The. Bat
</title>
<description>
<em>May 2, 2015</em><br />
Wom The Bat, 91, of city, formerly of Texas, died in the hospital. He was born in city, California. He is survived by two sons. Condolences: somewebsite.com.
</description>
<pubDate><DATE></pubDate>

</item>

<item>
<category>Obituary</category>
<title>Baz Foo Bar</title>
<description>
<em>April 30, 2015</em><br />
Baz Foo Bar, 88, of city, an Internal Revenue Service tax examiner, died at home. She was born in another city. She is survived by three daughters; 5 grandchildren; and 2 great-grandchildren. Services: 10:30 a.m. Private burial.
</description>
<pubDate><DATE></pubDate>
<FLAG>
</item>

<item>
<category>Obituary</category>
<title>John Doe</title>
<description>
<em>April 17, 2015</em><br />
John F. Doe, 51, of city, a structural engineer, died in the city. He was born in the Philippines. He is survived by his wife, son, daughter, and two parents. Visitation: 9:30 a.m. Monday. Eulogy: 10:30 a.m. Mass: 11:15 a.m. Burial: 1 p.m.
</description>
<pubDate><DATE></pubDate>
<FLAG>
</item>

<item>
<category>Obituary</category>
<title>Jane Smith
</title>
<description>
<em>May 1, 2015</em><br />
Jane "Mary" Smith, 64, of city, a storage specialist, died in the hospital. She was born in city, Rhode Island. She is survived by a son; two brothers; four sisters; and her caregiver. Graveside services: 11 a.m. Monday at cemetary. Urn burial to follow. Condolences: somewebsite.com.
</description>
<pubDate><DATE></pubDate>
</item>