PowerShell不规则xml文件的修改
有一个不规则的xml文件b.txt 内容如下:-------------------------------------------------------------------<Que K="Pro">
<Reference>[color=#ff0000]日[/color]</Reference>
<Reference>[color=#ff0000]本[/color]</Reference>
<Reference>[color=#ff0000]人[/color]</Reference>
</Que>
美国人
乌克兰人
俄罗斯人
-------------------------------------------------------------------
我想把 [color=#ff0000]日本人[/color] 三个字清空掉
如果直接用[xml]类型加载b.txt会报错, 正好刚刚从went大侠那里学了点正则用上了, 把xml的部分取出来了, 然后用xpath取到节点, 设置空值, 但是, 不知道怎么写回去了, 请求高手支招, 提前感谢!
代码如下:[code]$s = gc b.txt -enc UTF8 -raw
$s -match '(?s)<Que K="Pro">(.*?)</Que>'
if ($s -match '(?s)<Que K="Pro">(.*?)</Que>')
{ $x = $matches[0]
[xml]$x | Select-Xml -XPath "/Que/Reference" |
foreach { $_.node.InnerXML = "" }
}[/code] 用不到xml[code]$s -replace '(?<=<Reference>).*?(?=</Reference>)',''[/code] [b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=253697&ptid=62206]2#[/url] [i]went[/i] [/b]
感谢大侠指引, 这个小例子, 就是想知道有什么办法, 原位置写回去, 实际例子中, 节点处理比较多
如果能原位置写回去, 把匹配出来的这部分字符串, 进行搜索替换, 然后就方便多了, 甚至: 如果能定义为一个函数, 我给这个函数三个参数: 正则, 要搜的字符, 要替的字符, 它就能一步执行, 碉堡了 [code]$s -replace '(?<=<Reference>).*?(?=</Reference>)','新数据'[/code] [b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=253702&ptid=62206]4#[/url] [i]went[/i] [/b]
大侠的正则匹配+替换干净利索!
我上面提到的这个, 应用场景应该很多,
比如, 我不想全文去匹配, 我想先抽出来一部分区域[size=12px], 在其中进行操作[/size][size=12px], 缩小了范围更精准[/size]
[size=12px]我看正则的时候, 太吃力, 貌似也看到过, 可以把搜索到的内容, 替换回去, [/size]
[size=12px]我稍后整理下资料打印出来详细阅读下, 若有新发现, 贴上来, 供大家参考[/size] [b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=253702&ptid=62206]4#[/url] [i]went[/i] [/b]
我前面的代码有错, 现在可以写出去了, 但是没写回去, 呵呵
[code]$s = gc b.txt -enc UTF8 -raw
if ($s -match '(?s)<Que K="Pro">(.*?)</Que>')
{
[xml]$x = $matches[0]
$x | Select-Xml -XPath "/Que/Reference" |
foreach { $_.node.InnerXML = "" }
$x.save("new.txt")
}[/code] [i=s] 本帖最后由 5i365 于 2022-4-4 16:54 编辑 [/i]
[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=253702&ptid=62206]4#[/url] [i]went[/i] [/b]
大侠好, 我稍变了一下, 能用上面的正则修改一下,替换成下面的效果吗? 感谢前:
<Que K="Pro">
<Reference Relative="..\..[color=#ff0000]\Apple.Doc.docx[/color]">日</Reference>
<Reference Relative="..\..[color=#ff0000]\Apple.Li.docx[/color]">本</Reference>
<Reference Relative="..\..[color=#ff0000]\Apple.Pdf.docx[/color]">人</Reference>
</Que>
------------------------------------------------------------------------------------
后: 即分别删除了 日 本 人 再把前面标签里面属性值移进来
<Que K="Pro">
<Reference>[color=#ff0000]\Apple.Doc.docx[/color]</Reference>
<Reference>[color=#ff0000]\Apple.License.docx[/color]</Reference>
<Reference>[color=#ff0000]\Apple.Pdf.docx[/color]</Reference>
</Que> [code]$s -replace '(?<=<Reference)\s+Relative="\.\.\\\.\.(\\.*?)">(.*?)(?=</Reference>)',('>'+'$1')[/code][b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=253793&ptid=62206]7#[/url] [i]5i365[/i] [/b] [i=s] 本帖最后由 5i365 于 2022-4-5 14:01 编辑 [/i]
[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=253803&ptid=62206]8#[/url] [i]went[/i] [/b]
多谢大侠, 有点不太明白, $1 感觉好像指的第一个括号内的匹配, 能指点一下吗?
[font=Monaco, Consolas, "][size=12px][color=#ff0000](?<=<Reference)[/color][/size][/font][color=#c0be][font=Monaco, Consolas, "][size=12px]\s+Relative="\.\.\\\.\.[/size][/font][/color][font=Monaco, Consolas, "][size=12px][color=#00ff00](\\.*?)[/color][/size][/font][color=#c0be][font=Monaco, Consolas, "][size=12px]">[/size][/font][/color][font=Monaco, Consolas, "][size=12px][color=#9932cc](.*?)[/color][/size][/font][font=Monaco, Consolas, "][size=12px][color=#ff00ff](?=</Reference>)[/color][/size][/font][color=#c0be][font=Monaco, Consolas, "][size=12px]',('>'+'$1')[/size][/font][/color] -replace的$1 类比于 -match的$Matches[1]
页:
[1]