# Filtering files using regular expressions

**URL:** <https://discuss.gradle.org/t/filtering-files-using-regular-expressions/5613>\
**Category:** Old Forum Archive\
**Created:** [August 23, 2013, 9:44pm UTC](https://discuss.gradle.org/t/filtering-files-using-regular-expressions/5613 "2013-08-23T21:44:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon\_Murray](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/brandon_murray/32/1026_2.png) [@Brandon\_Murray](https://discuss.gradle.org/u/Brandon_Murray)\
**Post date:** [August 23, 2013, 9:44pm UTC](https://discuss.gradle.org/t/filtering-files-using-regular-expressions/5613/1 "2013-08-23T21:44:00Z")

</div>

Hello,

I’m converting the following ant code to gradle and I’m unable to figure out how to do the regex portion in gradle:

```gradle
<copy...>
                    <filterchain>
                        <striplinecomments>
                            <comment value="#"/>
                        </striplinecomments>
                        <tokenfilter>
                            <ignoreBlank/>
                        </tokenfilter>
                        <replaceregex pattern="\s*(\S+)\s*="
                            replace="@@KEY.\1@@="/>
                    </filterchain>
                </copy>

```

gradle code: …

```gradle
def regexp = new org.apache.tools.ant.types.RegularExpression()
    regexp.pattern = '[^ \n\t\r]+'
    filter(org.apache.tools.ant.filters.StripLineComments, comments: ["#"])
    filter(org.apache.tools.ant.filters.LineContainsRegExp, regexps:[regexp])
    // how to do regex replacement filter?

```

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Gary\_Hale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/gary_hale/32/7647_2.png) [@Gary\_Hale](https://discuss.gradle.org/u/Gary_Hale)\
**Post date:** [August 26, 2013, 3:03pm UTC](https://discuss.gradle.org/t/filtering-files-using-regular-expressions/5613/2 "2013-08-26T15:03:00Z")

</div>

I find the filter closure form the easiest to use for this sort of thing. You should be able to do something like this:

```gradle
project.task('myCopy', type: Copy) {
    from someDir
    into someOtherDir
    filter { line ->
      line.replaceAll("#.*","")
    }
  }

```

Point is, the filter closure gives you each line as as string where you can then manipulate it any way you want to and return the modified result.

---

<div class="post-metadata">

**Author:** ![Brandon\_Murray](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/brandon_murray/32/1026_2.png) [@Brandon\_Murray](https://discuss.gradle.org/u/Brandon_Murray)\
**Post date:** [August 26, 2013, 4:47pm UTC](https://discuss.gradle.org/t/filtering-files-using-regular-expressions/5613/3 "2013-08-26T16:47:00Z")

</div>

Thanks Gary. The filter closure did the trick:

```gradle
filter { String line ->
        line.replaceAll("\s*(\S+)\s*=", "@@KEY.\$1@@=")
    }

```

---

<div class="post-metadata">

**Author:** ![Naama\_Zuessman](https://avatars.discourse-cdn.com/v4/letter/n/22d042/32.png) [@Naama\_Zuessman](https://discuss.gradle.org/u/Naama_Zuessman)\
**Post date:** [June 21, 2014, 7:18am UTC](https://discuss.gradle.org/t/filtering-files-using-regular-expressions/5613/4 "2014-06-21T07:18:00Z")

</div>

Hi,

After playing around with it, this is what worked for me:

`
def param = new org.apache.tools.ant.types.Parameter()
param.type = ‘comment’
param.value = ‘*’
filter(StripLineComments, parameters:[param]) `
