ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eduardo Andrés Alfonso Sierra <ed_al...@spymac.com>
Subject tricky regular expression replacement (replaceregexp)
Date Fri, 30 Jul 2004 16:20:30 GMT
Hi

I want to replace a parameter in a file. The problem is that the location of
this parameter is undetermined. I mean, it can ben anywhere in the file. I
can locate it 'cause it is surrounded by two known tokens.

Sounds easy ! The problem occurs because the property can appear many times
in the file. I think it will be better understood using an example.

Suppose a xml file like this


<tag property="name">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="any" />
    <any_number_of_tags_even_zero />
</tag>
<tag property="name2">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="any" />
    <any_number_of_tags_even_zero />
</tag>
<tag property="name3">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="any" />
    <any_number_of_tags_even_zero />
</tag>



Now suppose that I want to change the tag
<THE_TAG_IM_LOOKING_FOR   value="any" />
for the one
<THE_TAG_IM_LOOKING_FOR   value="MY MEW VALUE" />
But I just wanna change the one that is within the tag with property = name



I tried this :

<replaceregexp file="file" flags="s">
    <regexp pattern="(&lt;tag
property=&quot;name&quot;&gt;)(.*)(&lt;THE_TAG_IM_LOOKING_FOR
value=&quot;any&quot; /&gt;)"/>
    <substitution expression="\1\2&lt;THE_TAG_IM_LOOKING_FOR value=&quot;MY
MEW VALUE&quot; /&gt;"/>
</replaceregexp>



Using flag="s" to match new line characters 'cause I don't know how many
lines there are before the tag I want to change.  The match pattern is:
first the tag with property="name" followed by any number of any character
(including new line characters) and then the tag I want to change.

The replace pattern is \1\2 to keep the text before the tag I want to change
and then the new tag value.

I supposed it works, I thought that first it match any text from the
beginning of the tag with property = name to the first occurrence of the tag
i want to change but it looks like it doesn't do that.

What it really did was to match any text from the beginning of the tag with
property = name to the LAST occurrence of the tag i want to change, so it
changed the value of the tag I want to change but the one within the tag
with property=name3.  So it changed the file and it was the result


<tag property="name">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="any" />
    <any_number_of_tags_even_zero />
</tag>
<tag property="name2">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="any" />
    <any_number_of_tags_even_zero />
</tag>
<tag property="name3">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="MY MEW VALUE" />
    <any_number_of_tags_even_zero />
</tag>


and what i wanted to do was


<tag property="name">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="MY MEW VALUE" />
    <any_number_of_tags_even_zero />
</tag>
<tag property="name2">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="any" />
    <any_number_of_tags_even_zero />
</tag>
<tag property="name3">
    <any_number_of_tags_even_zero />
    <THE_TAG_IM_LOOKING_FOR   value="ant" />
    <any_number_of_tags_even_zero />
</tag>


I have tried using the byline attribute true and then it doesn't match
anything.

WHAT CAN I DO ??
THANX A LOT !




Other example is I tried was using this file in the text

aaa b aaa c aaa d aaa e aaa

tried to change the aaa after the b and let the rest the same to obtain
this:  aaa b zzz c aaa d aaa e aaa

  <replaceregexp  file="../Version0_CVS/config/expresso-config.xml">
       <regexp pattern="b(.*)aaa"/>
       <substitution expression="b\1zzz"/>
  </replaceregexp>

It again replaced the last aaa occurence and not the one just after de b so
the result was

aaa b aaa c aaa d aaa e zzz



If someone could give me a working example for what I'm trying to do I would
appreciate very much ! I've tryied EVERYTHING !!!!



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message