简体   繁体   English

用蚂蚁脚本替换regexp

[英]replaceregexp with Ant Script

I have some problems with replaceregexp . 我对replaceregexp有一些问题。 To be more understandable, i'll use simple examples, and not the real code. 为了更容易理解,我将使用简单的示例,而不是真实的代码。

So presume I've 2 java files: 因此,假设我有2个Java文件:

firstClass.java 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 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. 因此,我想删除项目中每个java文件的“不必要的注释”部分,但我想使用ANT来完成此操作,而不是手动进行。 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 firstClass.java

/**
 */
public class firstClass{
    public String firstFunction(){
         <instructions>
    }

    /**
    * My function's description that I want to keep
    */
    public String secondFunction(){
         <instructions>
    }
}

secondClass.java secondClass.java

/**
 */
public class secondClass{
    public String firstFunction(){
         <instructions>
    }
}

It works very well for secondClass.java but not for the first file. 它对secondClass.java很好用,但对第一个文件却不好。 The problem appears when there are other comments, like functions' description. 当还有其他注释(例如功能说明)时,就会出现问题。 The file secondClass.java results well but in firstClass.java I get this: 文件secondClass.java结果很好,但是在firstClass.java中,我得到了以下信息:

/**
    */
    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 */. 所以问题是:当我的replaceregexp找到更多* /时,它将在最后一个* /处停止,并删除“不必要的评论”和最后一个* /之间的所有内容。

Do you have any solution to just delete what is between "UNDESIRABLE COMMENTS" and the first */ found? 您是否有任何解决方案,只需删除“不必要的评论”和找到的第一个* /之间的内容?

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 ie it will match as little as possible while still producing an overall match of the whole expression. 表示与+相同,但不愿意,即它会尽可能地匹配,同时仍会产生整个表达式的整体匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM