正则表达式删除文件中除字符串之外的所有内容

时间:2013-10-04 23:45:06

标签: regex notepad++

我正在尝试使用Notepad ++解析一些svg坐标。我想取每个图层的坐标集并将坐标放在[]中,以便准备好在javascript数组中使用。

svg文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build     43363)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"   "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="800px"
 height="900px" viewBox="0 0 800 900" enable-background="new 0 0 800 900"   xml:space="preserve">
<g id="Layer_3">
<polyline fill="none" stroke="#0000FF" stroke-width="3" points="33,108 66,141 99,174 99,207 132,207 165,207 165,240     "/>
</g>
<g id="Layer_4">
<polyline fill="none" stroke="#0000FF" stroke-width="3" points="132,306 165,306 165,339 165,372 132,405 99,405 99,438 132,438 
    165,438     "/>
</g>
<g id="Layer_5">
<polyline fill="none" stroke="#0000FF" stroke-width="3" points="198,339 231,339 264,372 297,372 330,405 363,438 396,438     "/>
</g>
<g id="Layer_6">
<polyline fill="none" stroke="#0000FF" stroke-width="3" points="198,174 198,273 231,306 264,306     "/>
</g>
<g id="Layer_7">
<polyline fill="none" stroke="#0000FF" stroke-width="3" points="231,174 231,240 264,273 297,273     "/>
</g>
<g id="Layer_9">
<polyline fill="none" stroke="#0000FF" stroke-width="3" points="396,306 462,306 495,339 495,372 528,405 528,438 561,438 
    594,471     "/>
</g>
<g id="Layer_10">
<polyline fill="none" stroke="#0000FF" stroke-width="3" points="660,504 561,504 495,504     "/>
</g>
</svg>

我希望结果看起来像这样:

[33,108 66,141 99,174 99,207 132,207 165,207 165,240]

[132,306 165,306 165,339 165,372 132,405 99,405 99,438 132,438 165,438]

[198,339 231,339 264,372 297,372 330,405 363,438 396,438]

[198,174 198,273 231,306 264,306]

[231,174 231,240 264,273 297,273]

[396,306 462,306 495,339 495,372 528,405 528,438 561,438 594,471]

[660,504 561,504 495,504]

我对正则表达式很新,但这是我的微弱尝试:

搜索:。+ points =“(。+”)

替换为:[$ 1]

我被卡住了,因为搜索部分似乎没有搜索“points”字符串上方的行。除了坐标,我怎么能摆脱一切,即使文件中有更高的垃圾?

1 个答案:

答案 0 :(得分:0)

首先,我必须与Ken White一致同意不使用正则表达式解析XML。但是如果你想在Notepad ++中使用快速而肮脏的解决方案,请试试这个(不要忘记检查“。匹配换行符”选项):

查找

.+?points="(.+?)\s*"

替换:

[$1]\n\n

这将几乎给你预期的结果(你仍然需要手动删除一些尾随的字符)。