开发者

Regx to delete a given pattern in string

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>


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>


<[^>:]*:\s*([^>]*)>

Search and replace with <$1>.


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.


(?<=<).*?:\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.


<.*?:\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>");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜