0

In the xml we have some tags like

<string1 : string2>

and many more like this.

i need to write a regular expression to delete all the string end with ":" and i.e. here string1 and ":" also. and it should be always inside < >

e.g. Input = <string1 : string2>

output = <string2>

stema
  • 90,351
  • 20
  • 107
  • 135
satyam
  • 11
  • 1
  • 3

5 Answers5

1

This is how you do it in php:

<?php
 $str = "<string1 : string2>";
 $s = preg_replace('~(</?)[^>:]*:\s*~', "$1", $str);
 var_dump($s);
?>

EDIT In Java

String str = "<ns2:senderId xmlns=\"netapp.com/fsoCanonical\">NetApp</ns2:senderId>";
System.out.println(str.replaceAll("(</?)[^>:]*:\\s*", "$1"));

Output

<senderId xmlns="netapp.com/fsoCanonical">NetApp</senderId>
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • @satyam: based on comments I have provided you one example in Java above. Hope that meets your requirement. – anubhava May 11 '11 at 15:01
0

<.*?:\s*(.*?)> should capture only the part you are interested in. How to do regex replacement can vary from programming language to programming language.

In java you could do

string.replaceAll("<.*?:\s*(.*?)>", "<$1>");
sverre
  • 6,768
  • 2
  • 27
  • 35
0
<[^>:]*:\s*([^>]*)>

Search and replace with <$1>.

Jeff
  • 13,943
  • 11
  • 55
  • 103
  • Hi jeff thanks a lot its working ,but one more exception.. lets say we have input xml like this NetApp Unisys 4CF4DC05126A0077E10080000A66C871 and we need the output as below – satyam May 11 '11 at 14:17
  • NetApp Unisys 4CF4DC05126A0077E10080000A66C871 – satyam May 11 '11 at 14:22
0

With an xslt parser you can use

<xsl:template match="*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="@*|text()|comment()|processing-instruction()">
    <xsl:copy-of select="."/>
</xsl:template>

This link is relevant for your question.

Community
  • 1
  • 1
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
0
(?<=<).*?:\s*(?=.*?>)

The look behind ensures the < is on the left, then matches your string including the : and optional whitespaces. The following look ahead ensures that there is the rest of the tag.

Capture this and replace with an empty string.

You can it see online here http://regexr.com

regular-expressions.info/java.html explains how to apply regexes in java.

stema
  • 90,351
  • 20
  • 107
  • 135