-1

I have a xml file with this data format

    <row Id="9" Body="aaaaaaaaa" Target="123456" />

I want to find & replace all Body="" things with a space from my xml file. What is the regex for that?

DilumN
  • 2,889
  • 6
  • 30
  • 44
  • 1
    Don't parse XML using regex; use a real parser. See [**How to retrieve element value of XML using Java?**](http://stackoverflow.com/questions/4076910/how-to-retrieve-element-value-of-xml-using-java) or [**How to read XML using XPath in Java**](http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java) – kjhughes Mar 02 '17 at 17:41

1 Answers1

0

There are many possibilities, here is one way to remove the content from the Body attribute

(<row.*Body=").*?("[^>]+>)

This creates two capturing groups for the content before and after the Body attribute. Then, you just use those capturing groups for the replacement:

$1$2

It will transform:

<row Id="9" Body="aaaaaaaaa" Target="123456" />

Into:

<row Id="9" Body="" Target="123456" />

You can see it working here.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • Can you give a regex only to find that element?? I can replace it by my text editor – DilumN Mar 02 '17 at 17:39
  • Sorry, I don't understand what you mean. You only want to find one element? With what characteristics? From your question it seems like you want to find all elements with an empty `Body` attribute. – nickb Mar 02 '17 at 17:43
  • Yes that's what I wanted. I have a data set of more than 20GB. That's why I wanted to remove this Body element data. I tried your answer with a text editor, but didn't work – DilumN Mar 02 '17 at 17:45
  • Oh okay, I think I understand - Like this? https://regex101.com/r/k2Y5zN/1 In the Substitution panel, I used `$1$2` as the replacement, so you can see that it turned `` into `` – nickb Mar 02 '17 at 17:48
  • 1
    This is exactly what I wanted. It's great if you can edit the answer with this code as well – DilumN Mar 02 '17 at 17:52