tags:

views:

31

answers:

1

I've got a XML file and I need to locate all parameters with parameterid named aspect (or name field with Aspect as the value), find the end of the parameter, insert than another similar parameter called linewidth (see below for the aspect parameter

  <parameter>
   <parameterid>aspect</parameterid>
   <name>Aspect</name>
   <valuemin>0.1</valuemin>
   <valuemax>5</valuemax>
   <value>1</value>
  </parameter>

Right now, I can do it with the below code:

(?m:(<name>Aspect<.+?</value>.))(\t+)(</parameter>)

$1$2$3\n$2<parameter>\n$2\t<parameterid>linewidth</parameterid>\n$2\t<name>Line Width</name>\n$2\t<valuemin>0</valuemin>\n$2\t<valuemax>200</valuemax>\n$2\t<value>20</value>\n$2</parameter>

However, I'm pasting all the aspect parameter first. I tried to use (?<= so that I can just find the location of the end of the aspect parameter. But once I do that the (?m: doesn't work. Does anyone know how to make them work together? Just want to know.

Ideally if something like this works would be great:

(?m:(?<=<name>Aspect<.+?</value>.))(\t+)(</parameter>)

Thanx in advance.

A: 

Try this regex:

(?m:(<name>Aspect<.+?</value>))(\s+)(</parameter>)

The (\s+) captures both the linefeed and the spaces at the beginning of the next line, so you'll probably want to remove all those \n and \t escapes from the replacement string. The inserted text isn't properly indented, but the regex works.

Alan Moore
Thank you for the \s suggestion, I didn't know that. Do you know how to make the (?m: and (?<= work together? Is it not possible. I just realized that my question is flawed. The aspect parameter is one of the many parameters in the xml file. So I'm looking to just insert another parameter after the aspect parameter. So, I guess it should be something like (?m:(?<=<name>Aspect<.+?\s+</parameter>))(\s<parameter>) or a way to find the insert point right after the ending of the aspect parameter
T1000
It isn't the `.` that's messing you up, it's the `+`. As in most other flavors, Oniguruma's (TextMate's regex engine) lookbehinds need to know beforehand how many characters they're going to match, and the `+` makes that impossible.
Alan Moore
I see. lookaround does not work with repetition. Thank you so much. So, do you think there is another way to achieve what I'm trying to do? By that, I mean to insert a xml node behind a specific xml node in a list of xml nodes and not by copying the identified node, but to find the insert point to insert the node.
T1000