regex - ant propertyregex,用指定字符串替换字符串

标签 regex ant replace

这是我的字符串com.element.subelement

我想使用 propertyregex 将所有 . 替换为 /,并将该输出用作目录路径。

看起来 propertyregex 就是解决这个问题的工具,但也许我错误地使用了它的 regexp 属性。

我如何使用正则表达式来查找 . ,我不想假设该字符串是由句点分隔的 3 个字母部分。也许 propertyregex 也不是完成这项工作的工具

这里是一个可行的方法,我假设包名称只有 2 个句点

<!-- set package name to folder directory -->
    <propertyregex 
        property="current.target.dir"
          input="com.element.subelement"
          regexp="(.+?)\.(.+?)\.(.+?)"
          replace="\1/\2/\3"
          casesensitive="false" />

    <echo>${current.target.dir}</echo>

最佳答案

如果我理解的话,只需将 \. 替换为 / 就可以完成您正在寻找的内容。但是,默认情况下,这只会替换一行中的第一个实例,因此您必须添加属性 global="true":

<propertyregex 
    property="current.target.dir"
      input="com.element.subelement"
      regexp="\."
      replace="/"
      global="true" />

关于regex - ant propertyregex,用指定字符串替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848515/

相关文章:

c# - 如何使此正则表达式正确匹配?

regex - 特定日期格式的正则表达式

java - 使用 Maven Ant 任务执行 jetty stop 时未授予权限 (java.lang.RuntimePermission exitVM)

tomcat - 使用 Ant 启动和停止 Tomcat 服务

Python - 用本身加上特殊字符替换字符串而不考虑大写和小写

c# - 如何标记空格和引号字符串,处理转义?

使用正则表达式解析 Java 字符串

java - 带有 Subversion : build after commit on Windows using Java or Ant 的 Hudson

c# - 用空字符串替换json字符串中的双引号

javascript用数组全局替换