Link to home
Start Free TrialLog in
Avatar of HumanContent

asked on

How to write a complicated replace function?

I have a text that has a few codes like this:
<verse sID="Gen.1.3" n="3" osisID="Gen.1.3" /> text text text text
<verse subType="x-embedded" sID="Gen.1.4" n="4" osisID="Gen.1.4" />  text text text text

Can anybody help me with a vb code to replace the above codes to:"
<verse sID="Gen.1.3" n="3" osisID="Gen.1.3"> text text text text
<verse subType="x-embedded" sID="Gen.1.4" n="4" osisID="Gen.1.4">  text text text text

So without the /> in the end. The problem is i cannot use replace("/>",">", text) because there are other items that use the /> as well and these need to be kept. So it only has to replace all the codes that begin with <verse ....
Can anybody help me on this one?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Is this HTML text or XML text?

Assuming Test contains the string you wish to process.  This code requires an import.

Imports System.Text.RegularExpressions

Test = Regex.Replace(Test, "(osisID=""Gen.1.(3|4)"")(\s)(/>)", "$1>")

Open in new window

Avatar of HumanContent


The code is XML, so i want to change a xml document into xml i can prcess easier.
Test = Regex.Replace(Test, "(osisID=""Gen.1.(3|4)"")(\s)(/>)", "$1>")
This will not work, because the text between "<verse" and the "/>" is to different every time. So the only common thing is that they all start with: "<verse"
Avatar of PaulHews
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, a regular expression would be the right way to go, since you can't use XmlDocument to process elements with self-closing tags (they won't be processed correctly.

>The code is XML, so i want to change a xml document into xml i can prcess easier.

This I'm not sure I understand....  If it's XML then the <tag /> indicates a node with no child elements.  It will not be easier or harder to process.
>(they won't be processed correctly.

In what way?
thanx excatcly what i was looking for
You can select childless nodes and read their attributes easily enough using xpath.  

Also, if you are replacing the /> with > then you also have to close the tag to create valid XML.  
The XML i have is osis, and that works different than normal XML, thanks for your answer.