eclipse搜索替换正则表达式迭代

时间:2013-07-09 09:09:36

标签: xml regex expression

我想要替换

step="1" step="2" step="5" step="33"

以下

step="1" step="2" step="3" step="4"

我希望替换的数字能够将其迭代次数校正为1。

到目前为止我的发现:

step="\d"

工作正常。 我可以用什么代替?

1 个答案:

答案 0 :(得分:0)

如果您正在使用ant并在项目中包含ant-contrib 您可以使用<math>任务来增加var的值。

假设${STEP_STRING} = step="1" step="2" step="5" step="33" 生成${NEW_STRING} = step="1" step="2" step="3" step="4"

<var name="ctr" value="1"/>
<var name="ctr_str" value="step"/>
<var name="NEW_STRING" value=""/>

<for list="${STEP_STRING}" delimiter="${ctr_str}" param="str">
   <sequential>          
      <condition property="true">
          <matches string="@{str}" pattern="=&quot;\d&quot;"/>
      </condition>
      <if><isset property="true"/>
          <then>
             <propertyregex property="xx" input="@{str}" regexp="(=&quot;)\d(&quot;)" replace="\1${ctr}\2" override="true"/>
             <var name="NEW_STRING" value="${NEW_STRING} ${ctr_str}${xx}"/>
             <math result="ctr" operand1="${ctr}" operand2="1" operation="+" datatype="int" />
          </then>
      </if>
   </sequential>
</for>
<echo>${NEW_STRING}</echo>  

我认为这会生成所需的字符串,虽然我还没有对其进行测试,但它构建一个新字符串而不是替换之前的字符串字符串(您正在寻找)。在选择Nth occurrence of ="\d" 时,替换变得复杂。 我认为你需要一个自定义的<scriptdef>来完成这个任务,我会在此期间尝试写这个任务。