Reputation: 107
I can't find the way to remove a space using ant. (I don't mean trim, but removing from ~ to )
I want to make one project a little difference from another project.
For example:
I have below code.
.... doSomeThing;
//#removeStart
NotReleasedObject nro = new NotReleasedObject();
nro.doSomeThing();
//removeEnd#
....doAnyThing..
as you can see easily, I want to remove from //#removeStart
to //removeEnd#
. I'm not sure how much code will be written, but all code inside these 2 tags need to be gone.And there can be multiple //#removeStart ... //removeEnd#
in one java file.
Here is my test replaceregexp
code:
<replaceregexp flags="s" replace="" byline="false" encoding="utf-8">
<regexp pattern="//#removeStart.*//removeEnd#"/>
<fileset dir="${extern-source-path}"/>
</replaceregexp>
------ source for victim.
//#removeStart
space 1
//removeEnd#
something
//#removeStart
space 2
//removeEnd#
Now my code is not working properly.The flag -s works, but if one file has more than 1 match, then everything from space 1 to space 2 is gone. ( including something
!)
What I actually want is space 1,2 gone, but something
should be left as it is.
How can I fix it so that it replaces each occurrence 1 by 1 ?
Upvotes: 2
Views: 2300
Reputation: 107040
I've successfully did it with this:
This is a test
--->
###start remove
Blah blah blah
Yadda yadda yadda
###end remove
--->
End of my test
<project>
<replaceregexp file="foo.txt"
flags="s"
match="###start remove.+###end remove"
replace=""/>
</project>
This is a test
--->
--->
End of my test
It looks like your regex are not quite right. You're regex is saying ##ExternRemove.*
, but your file has //#removeStart
instead.
I was seeing if I could do this in a copy or concat, because I hate changing files directly. Unfortunately, I hadn't figured out a way to do this.
Upvotes: 0
Reputation: 17076
Your explicit characters don't match: your regex says //##ExternRemove
whereas your actual text is either //##removeStart
or //#removeStart
. If that's an actual regex, the explicit characters need to be exactly the same. Also .
doesn't match newlines, you'd need to include that specifically (e.g. [\s\S]*
).
Finally, you should make the matching non-greedy if you might have multiple pairs of this match (often this is by adding ?
). Taken together, you'd probably want a regular expression like:
//##?removeStart[\s\S]*?//removeEnd##?
That's assuming you want to allow 1-2 #
to start a line and that the phrase will always be removeStart
/removeEnd
. Otherwise you could add options, e.g.
//##?(?:removeStart|ExternRemove)[\s\S]*?//(?:removeEnd|ExternEnd)##?
again, this depends on the ant terminology for non-capturing matches.
Upvotes: 2