Reputation: 285
I have some problems with replaceregexp
. To be more understandable, i'll use simple examples, and not the real code.
So presume I've 2 java files:
firstClass.java
/**
* UNDESIRABLE COMMENTS ABCDEFGHIJ ABCDEFGHIJ ABCDEFGHIJ ABCDEFGHIJ ABCDEFGHIJ
* ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
*/
public class firstClass{
public String firstFunction(){
<instructions>
}
/**
* My function's description that I want to keep
*/
public String secondFunction(){
<instructions>
}
}
secondClass.java
/**
* UNDESIRABLE COMMENTS ABCDEFGHIJ ABCDEFGHIJ ABCDEFGHIJ ABCDEFGHIJ ABCDEFGHIJ
* ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
*/
public class secondClass{
public String firstFunction(){
<instructions>
}
}
So I want to remove the "UNDESIRABLE COMMENTS" part of each java file in my project, but I want to do it with ANT, not manually. For that, i'm using this:
<replaceregexp flags="s">
<regexp pattern="UNDESIRABLE COMMENTS.+\*/"/>
<substitution expression="*/"/>
<fileset dir=".">
<filename name="*.java"/>
</fileset>
</replaceregexp>
What I want is to get this code:
firstClass.java
/**
*/
public class firstClass{
public String firstFunction(){
<instructions>
}
/**
* My function's description that I want to keep
*/
public String secondFunction(){
<instructions>
}
}
secondClass.java
/**
*/
public class secondClass{
public String firstFunction(){
<instructions>
}
}
It works very well for secondClass.java but not for the first file. The problem appears when there are other comments, like functions' description. The file secondClass.java results well but in firstClass.java I get this:
/**
*/
public String secondFunction(){
<instructions>
}
}
Everything what was between the beginning of "UNDESIRABLE COMMENTS" to the end of my second function's description was deleted.
So the problem is: when my replaceregexp finds more */, it stops on the last one, and delete everything between "UNDESIRABLE COMMENTS" and this last */.
Do you have any solution to just delete what is between "UNDESIRABLE COMMENTS" and the first */ found?
Upvotes: 2
Views: 579
Reputation: 122364
Your problem is that that +
quantifier in regular expressions is greedy, and will consume as much of the input as possible while still matching. Try
UNDESIRABLE COMMENTS.+?\*/
instead - +?
means the same as +
but is reluctant i.e. it will match as little as possible while still producing an overall match of the whole expression.
Upvotes: 1