Nishant Sharma
Nishant Sharma

Reputation: 55

how to replace all substrings whose starting and ending is known

The following string

"<service id="any value"></service>"

I want to replace it with the following using regex in java

 <service></service>

I have written the following but is not working if there are more than one service tag in the string

string.replaceAll("^<service.*</service>$", "<service></service>");

Example :

<request>
  <service id="anyvalue"></service>
  <service id="anyvalue"></service>
</request>

Upvotes: 0

Views: 72

Answers (3)

SomeDude
SomeDude

Reputation: 14238

Using regex to modify your xml is always going to be a problem, let us assume that your regex <service.*</service> works currently for your xml. Later you want to update your xml to include a new attribute called name as :

<request>
  <service id="anyvalue" name="myservice"></service>
  <service id="anyvalue" name="myservice"></service>
</request>

and you want to remove id attribute only.

What do you think the regex will do ? It will remove name as well.

I would always use an xml parser to modify xml.

The following code would do it :

try
{
    String input = "<request><service id=\"anyvalue\"></service><service id=\"anyvalue\"></service></request>";
    InputSource inputSource = new InputSource( new StringReader( input ) );

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse( inputSource );

    NodeList nodes = doc.getElementsByTagName("service");

    for ( int i = 0; i < nodes.getLength(); i++ )
    {
        Node node = nodes.item(i);
        if ( node.getNodeType() == Node.ELEMENT_NODE )
        {
            Element element = (Element)node;
            element.removeAttribute("id");
        }
    }

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
    transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "true" );

    DOMSource xmlSrc = new DOMSource( doc.getDocumentElement() );
    StringWriter sw = new StringWriter();
    transformer.transform( xmlSrc, new StreamResult( sw ));
    System.out.println( "Modified xml is : " + sw.toString() );       

}
catch ( Exception e )
{
    e.printStackTrace();
}

Upvotes: 0

Mike Adamenko
Mike Adamenko

Reputation: 3002

Use following regexp:

<service id=[^=]*<\/service>

It works correct with more than one service tag

See here

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59988

You can use the regex without ^ and $ in the start and end <service.*</service> so if you use :

string.replaceAll("<service.*</service>", "<service></service>")

result :

<request>
  <service></service>
  <service></service>
</request>

Upvotes: 3

Related Questions