代码之家  ›  专栏  ›  技术社区  ›  Lark

如何在Ant中提取子字符串

ant
  •  39
  • Lark  · 技术社区  · 17 年前

    是否有方法从Ant属性中提取子字符串并将其放入自己的属性中?

    6 回复  |  直到 17 年前
        1
  •  39
  •   Community Mohan Dere    15 年前

     <project>
      <scriptdef name="substring" language="javascript">
         <attribute name="text" />
         <attribute name="start" />
         <attribute name="end" />
         <attribute name="property" />
         <![CDATA[
           var text = attributes.get("text");
           var start = attributes.get("start");
           var end = attributes.get("end") || text.length();
           project.setProperty(attributes.get("property"), text.substring(start, end));
         ]]>
      </scriptdef>
      ........
      <target ...>
         <substring text="asdfasdfasdf" start="2" end="10" property="subtext" />
         <echo message="subtext = ${subtext}" />
      </target>
     </project>
    
        2
  •  23
  •   Peter Lang    8 年前

    PropertyRegex from Ant-Contrib .

       <propertyregex property="destinationProperty"
                  input="${sourceProperty}"
                  regexp="regexToMatchSubstring"
                  select="\1"
                  casesensitive="false" />
    
        3
  •  10
  •   Bill Birch    14 年前

    由于我更喜欢使用vanilla Ant,所以我使用了一个临时文件。适用于任何地方,您可以利用replaceregex来删除字符串中的一部分 想要。修改Git消息的示例:

        <exec executable="git" output="${git.describe.file}" errorproperty="git.error" failonerror="true">
            <arg value="describe"/>
            <arg value="--tags" />
            <arg value="--abbrev=0" />
        </exec>
        <loadfile srcfile="${git.describe.file}" property="git.workspace.specification.version">
            <filterchain>
               <headfilter lines="1" skip="0"/>
               <tokenfilter>
                  <replaceregex pattern="\.[0-9]+$" replace="" flags="gi"/>
               </tokenfilter>
               <striplinebreaks/>
            </filterchain>
        </loadfile>
    
        4
  •  6
  •   Markus Rohlof    6 年前

    我想一个简单的香草方法是:

    <loadresource property="destinationProperty">
        <concat>${sourceProperty}</concat>
        <filterchain>
            <replaceregex pattern="regexToMatchSubstring" replace="\1" />
        </filterchain>
    </loadresource>
    
        5
  •  2
  •   Vladimir    17 年前

    public class SubstringTask extends Task {
    
        public void execute() throws BuildException {
            String input = getProject().getProperty("oldproperty");
            String output = process(input);
            getProject().setProperty("newproperty", output);
        }
    }
    

    剩下什么来实施 String process(String) oldproperty 和 newproperty 值)

        6
  •  0
  •   Rebse    15 年前

    为此,我会使用脚本任务,我更喜欢ruby,例如 剪掉前3个字符=

    <project>
      <property name="mystring" value="foobarfoobaz"/>  
       <target name="main"> 
        <script language="ruby">
         $project.setProperty 'mystring', $mystring[3..-1]
        </script>
        <echo>$${mystring} == ${mystring}</echo>
       </target>    
      </project>
    

    输出=

    main:
         [echo] ${mystring} == barfoobaz
    

    将ant api与project.setProperty()方法一起用于现有的 属性将覆盖它,这样您就可以绕过标准ant了

    推荐文章