6

I have the following function that I am using to remove the characters \04 and nulls from my xmlString but I can't find what do I need to change to avoid removing the \ from my ending tags. This is what I get when I run this function

<ARR>20080625<ARR><DEP>20110606<DEP><PCIID>626783<PCIID><NOPAX>1<NOPAX><TG><TG><HASPREV>FALSE<HASPREV><HASSUCC>FALSE<HASSUCC>

Can anybody help me find out what do I need to change in my expression to keep the ending tag as </tag>

Private Function CleanInput(ByVal inputXML As String) As String
    ' Note - This will perform better if you compile the Regex and use a reference to it.
    ' That assumes it will still be memory-resident the next time it is invoked.
    ' Replace invalid characters with empty strings.
    Return Regex.Replace(inputXML, "[^><\w\.@-]", "")
End Function
5
  • This does not remove '\0' and '\04' characters from your string, but rather removes everything except a few characters (<, >, whitespace, ., @ and -). Also, what is the input that gives the output that you mention? Commented Mar 24, 2010 at 16:11
  • Can you post a line or two of what the input to this function looks like? Commented Mar 24, 2010 at 16:12
  • @Thomas, \w is word characters, not whitespace. Commented Mar 24, 2010 at 16:14
  • 1
    Whoops! Which once again proves than regexes were made to be written, not read ;) Commented Mar 24, 2010 at 16:37
  • +1 for coming up with a case where using regular expressions and XML together was not a WTF. ;) Commented Mar 24, 2010 at 17:22

1 Answer 1

4
Private Function CleanInput(ByVal inputXML As String) As String
    Return Regex.Replace(inputXML, "[^/><\w\.@-]", "")
    ' --------------------------------^
End Function

But since your target is only removing the \04 and \00's it's safer to restrict the replacement on them only.

Private Function CleanInput(ByVal inputXML As String) As String
    Return Regex.Replace(inputXML, "[\4\0]", "")
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch! everybody for your input. I am getting a clean XML now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.