0

I have xml like this and i want to replace carrot tag value by something else i use this expression (<.[^(><.)]+>) but it doesn't work properly what should i change to manage getting value and replacing it with new value?:

<Monkey xmlns="http://urlhere.com/monkeynamespace">
 <foodType>
  <vegtables>
   <carrots>1</carrots>
  </vegtables>
 <foodType>   
</Monkey>
Paula Livingstone
  • 1,175
  • 1
  • 12
  • 21
Sagitarius
  • 348
  • 1
  • 11
  • 36
  • 3
    [**TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ**](https://stackoverflow.com/a/1732454/1954610) – Tom Lord Oct 04 '17 at 10:52
  • 1
    Use an XML parser. Not regex. What language are you using? – Tom Lord Oct 04 '17 at 10:52
  • i want to make it in nifi replaceText processor and it doesn't support xpath i, i use java – Sagitarius Oct 04 '17 at 11:01
  • Don't parse XML using regex; use a real XML 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 Oct 04 '17 at 15:53

1 Answers1

8

You could use a positive lookahead and a positive lookbehind

Example:

(?<=<carrots>).*(?=<\/carrots>)

Demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70