问题描述
我在多个由不同值组成的 Value 系列中出现了 string 标记。我需要使用 regexp_replace()
进行通配符搜索,读取所有此类 string 出现并用通用值“NULL”替换它们。
下面是一个示例 XML:
<ParentArray>
<ParentFieldArray>
<Value>
<string>123</string>
<string>234</string>
</Value>
</ParentFieldArray>
<ParentFieldArray>
<Value>
<string>345</string>
<string>456</string>
</Value>
</ParentFieldArray>
</ParentArray>
期望是通读所有 String 标记值并将它们替换为 NULL。
解决方法
使用
regexp_replace(str,'(<string>)(\\d+)(</string>)','$1NULL$3')
演示:
select "<ParentArray>
<ParentFieldArray>
<Value>
<string>123</string>
<string>234</string>
</Value>
</ParentFieldArray>
<ParentFieldArray>
<Value>
<string>345</string>
<string>456</string>
</Value>
</ParentFieldArray>
</ParentArray>
" as str)
select regexp_replace(str,'$1NULL$3') from mydata
结果:
<ParentArray>
<ParentFieldArray>
<Value>
<string>NULL</string>
<string>NULL</string>
</Value>
</ParentFieldArray>
<ParentFieldArray>
<Value>
<string>NULL</string>
<string>NULL</string>
</Value>
</ParentFieldArray>
</ParentArray>
如果您不仅要替换值中的数字,包括空值,请使用:
select regexp_replace(str,'(<string>)(.*)(</string>)','$1NULL$3') from mydata