长标题:)
无论如何,我有很多XML文件,我希望在运行中清理,使用PHP preg_replace RegEx输出进行简单的运行转换。
现在我无法使更改永久化,所以我编写了一个php函数来遍历该文件。
我无法修复的是RegEx模式。
https://regex101.com/r/bN5eF4/7
我想匹配:
<all-tags with-their="attribute"
even-if-there="are-more">
and all the content between the start and end tag
even if there
are line breaks
in between them
</all-tags>
我敢打赌这很简单,但我从来没有很好地处理过RegEx。。。悲哀地
已编辑
似乎有人想让我构建一个SimpleXML的解析器函数,通过xml文件并删除换行符?
在同一过程中,我想删除一些元素及其内容,这取决于它们的属性剖析可以这么说。
我认为在用Xsltprocessor处理xml文件之前进行换行和分析会是更快的选择吗?
我成功地使用了2个正则表达式模式
输入:
<all-tags
with-their="attribute"
even-if-there="are-more"
aa="1">
and all the content between
the start and end tag
</all-tags>
<meta-tag />
1.删除打开标记之前和关闭标记之后的换行符https://regex101.com/r/PPzkWv/2/
/(?<=>)(n+)|(n+)(?=<)/
输出:
<all-tags
with-their="attribute"
even-if-there="are-more"
aa="1">and all the content between
the start and end tag</all-tags><meta-tag />
2.在不破坏语义的情况下,从输出中删除标记内的换行符https://regex101.com/r/GvBc7J/3/
/(s?n+s+|n)/
最终输出:
<all-tags with-their="attribute" even-if-there="are-more" aa="1">and all the content between the start and end tag</all-tags><meta-tag />
尝试以下正则表达式:
/(?<=>)(r?n)|(r?n)(?=</)/
在这里,您在>
的末尾或</
的开头搜索换行符,并替换它将为空字符串。
查看Regex101 演示
根据您的示例输入文本,它将删除所有换行符并将内容发送为:
<all-tags with-their="attribute" even-if-there="are-more">and all the content between the start and end tag</all-tags>