Jelle De Loecker
Jelle De Loecker

Reputation: 21945

Replace text with other text in the same line

I don't know if I can use regex for this, but I want to replace something in this xml:

<custom-attribute name="Attribute_1" dt:dt="string">Danny Boyle</custom-attribute>
<custom-attribute name="DVD-releasedatum" dt:dt="string">06/10/1999</custom-attribute>

should become

<Attribute_1>Danny Boyle</Attribute_1>
<DVD-releasedatum>06/10/1999</DVD-releasedatum>

Removing this from the first tag isn't hard, but how can I close my newly formed tag?

Upvotes: 0

Views: 244

Answers (5)

Jens
Jens

Reputation: 25563

If you want to do this once, Regex replace can be an option. Otherwise, there are better ways to transform XML, XSLT, for example.

For using Regex, you could replace

\<custom-attribute.*?name="(\w+)".*?\>(.*?)\</custom-attribute\>

with

<$1>$2</$1>

Replace $1 and $2 with whatever references are called in you program. Save a backup first, though =)

Upvotes: 1

Andy Shellam
Andy Shellam

Reputation: 15535

This works for C# (not sure what language you're using):

string input = "<custom-attribute name=\"Attribute_1\" dt:dt=\"string\">Danny Boyle</custom-attribute>\r\n<custom-attribute name=\"DVD-releasedatum\" dt:dt=\"string\">06/10/1999</custom-attribute>";

string output = Regex.Replace(input, "<custom-attribute name=\"(.*?)\".*?>(.*?)</custom-attribute>", "<$1>$2</$1>");

output:

<Attribute_1>Danny Boyle</Attribute_1>
<DVD-releasedatum>06/10/1999</DVD-releasedatum>

Upvotes: 1

Jakob
Jakob

Reputation: 2746

This looks like a job for XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="custom-attribute">
        <xsl:element name="{@name}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

This gives you the desired output, and is very flexible for future modification and extension.

Upvotes: 2

muruga
muruga

Reputation: 2122

while(<DATA>)
{
  if($_=~s/^\<.*=\"(.*)\" .*\>([a-zA-Z]+|[0-9\/ ]+).*/<$1>$2<\/$1>/)
  {
      print $_;
  }
 }
__DATA__
<custom-attribute name="Attribute_1" dt:dt="string">Danny Boyle</custom-attribute>
<custom-attribute name="DVD-releasedatum" dt:dt="string">06/10/1999</custom-attribute>

Upvotes: 1

Tomislav Nakic-Alfirevic
Tomislav Nakic-Alfirevic

Reputation: 10173

Using e.g. gvim, this will do it:

:%s/.*name="\([^"]*\)"[^>]*>\([^<]*\)<.*/<\1>\2<\/\1>/cg

This is the matching part:

.*name="\([^"]*\)"[^>]*>\([^<]*\)<.*

This is the replace part:

<\1>\2<\/\1>

Upvotes: 1

Related Questions