RegExp, removing dots in tags

I searched and searched, but just can't find a solution.
I need to remove dots in XML document tags with RegExp in C # ....

for example:

test <12.34.56>test.test<12.34>

it should be:

test <12346>test.test<1234>

So basically removing points, but only in tags .... any ideas?

+5
source share
2 answers
resultString = Regex.Replace(subjectString, @"\.(?=[^<>]*>)", "");

replaces a point with an empty string only if the next next angle bracket is the closing angle bracket.

This, of course, is fragile, because brackets with a closing angle may appear between the tags in the text, but if you are sure that this will not happen, you should be fine.

Explanation:

\.      # Match a dot
(?=     # only if the following regex can be matched at the current position:
 [^<>]* #  - zero or more characters except < or >
 >      #  - followed by a >
)       # End of lookahead assertion
+5
source

I would use an XML parser for it

XDocument xdoc = XDocument.Load(new StringReader("<root><s123.45><s678.9>aaaa</s678.9></s123.45></root>"));
foreach (var elem in xdoc.Descendants()) 
    elem.Name = elem.Name.LocalName.Replace(".", "");
Console.WriteLine(xdoc);
+2
source

All Articles