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 Re: tricky regular expression replacement (replaceregexp)
Date Fri, 30 Jul 2004 18:55:19 GMT
Thanx a lot Bill!!

It worked perfect !

I did just the little change you suggested,  I used (.*?) insetad of (.*)
and thats the way it works ok !!

Thanx again !!




----- Original Message ----- 
From: "Bill Rich" <billrich@attglobal.net>
To: "'Ant Users List'" <user@ant.apache.org>; "'Eduardo Andrés Alfonso
Sierra'" <ed_alfon@spymac.com>
Sent: Friday, July 30, 2004 12:26 PM
Subject: RE: tricky regular expression replacement (replaceregexp)


It looks like you are getting the greedy string and you want the reluctant
string.  In most cases regular expressions will get the longest string that
matches the expression.  If you want the shortest string you need to modify
the rep character.  I think it is usually "*?".  Try changing the reg exp to
(untested)
<regexp pattern="(&lt;tag
property=&quot;name&quot;&gt;)(.*?)(&lt;THE_TAG_IM_LOOKING_FOR
value=&quot;any&quot; /&gt;)"/>

HTH

Bill Rich
Wilandra Consulting LLC
1325 Addiewell Place
San Jose, CA  95120-3905
phone:      +1 408 268-2452
mobile:     +1 408 410-9713
Santa Cruz: +1 831 464-9007
fax:        +1 413 669-9716
billrich@wilandra.com or billrich@attglobal.net
http://www.wilandra.com

-----Original Message-----
From: Eduardo Andrés Alfonso Sierra [mailto:ed_alfon@spymac.com]
Sent: Friday, July 30, 2004 9:21 AM
To: Ant Users List
Subject: tricky regular expression replacement (replaceregexp)

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




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




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


Mime
View raw message