开发者

Regex to repair malformed XML attributes, removing spaces in tags

I've been unfort开发者_开发百科unate to have come accross alot of malformed XML.

I cannot get the correct regex to remove 2 spaces inside the attribute/key.

My current regex also checks to see if there is a valid "=" attribute.

XML attributes have to have a value or / and just one key.

for example

<ImValid></ImValid>
<Im not Valid></Im not Valid>
<ImValid with="somthing"></ImValid>

Here is my malformed XML:

<Addresses>
  <Address>
    <Delivery id>123123</Delivery id>
    <Delivery Code Id>123123</Delivery Code Id>
    <Full Name>Agent Smith</Full Name>
  </Address>
  <Address>
    <Delivery id>12322123</Delivery id>
    <Delivery Code Id>12zz3123</Delivery Code Id>
    <Full Name>Mr Anderson</Full Name>
  </Address>
<Addresses>

I'm trying to repair it using regex.

AstringVar => Regex.Replace(AstringVar , @"(?=<[^=]+?>)(?=</?\w+\s+\w+)(<.*?)(\s+)(.*?>)", @"$1$3", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)

This Will Change This

<Full Name>Mr Anderson</Full Name>

This

<FullName>Mr Anderson</FullName>

Improving .. But also missing the last space..

<DeliveryCode Id>12zz3123</DeliveryCode Id>

Ok...i could run it twice ... but.. that seems ugly... how would I be able to get both 1 space and 2 spaces whilst also avoiding the values Thanks to any regex heros who can help ...!

Here it is on regex101https://regex101.com/r/dVs51I/3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜