-1

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>
4

1 Answer 1

9

You could use a positive lookahead and a positive lookbehind

Example:

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

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

this will fail if there is another </carrots> down the string
need to add another handle, if there is multiple carrots on the string
That is correct. In that case you should make it lazy using .*?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.