Chapter 22. Grep Task
Prev | Additional Ant Tasks | Next

## Chapter 22. Grep Task

This task uses a regular expression to do pattern matching against a string and store the match in a property. This is useful for extracting a substring, or checking that an html form contains a particular value.

To use this task in your build files, include a task definition like this:

        <taskdef name="grep" classname="ise.antelope.tasks.Find"/>

Table 22.1. Grep Task Attributes

Attribute | Description | Default | Required
--- | --- | --- | ---
in | The string to perform the regular expression matching on | None | Yes
regex | The regular expression. See the Java API documentation for java.util.regex.Pattern for the details of the syntax for this expression. | None | Yes
group | The regular expression group to return in the property. | 0 | No
property | The name of a property in which to put the matched value. | None | Yes
allmatches | A regex may find multiple matches in the input string. If this attribute is set to true, then the property set after the grep will contain all matches. The individual matches can be separated by using the 'separator' attribute (see below). The default is 'false', that is, only return the first match. | false | No
separator | When 'allmatches' is set to true and there are multiple matches, this value will be used to separate the individual matches. | ${line.separator} | No
caseinsensitive | Enables case-insensitive matching. | false | No
comments | Permits whitespace and comments in pattern. | false | No
dotall | Enables dotall mode. | false | No
multiline | Enables multiline mode. | false | No
unicodecase | Enables Unicode-aware case folding. | false | No
canoneq | Enables canonical equivalence. | false | No
unixlines | Enables Unix lines mode. | false | No

Examples:

     <target name="test" description="Test grep">
          <grep in="${response}" regex="(account id=)([0-9]+)" group="2" property="AccountId"/>
          <echo>Account Id: ${AccountId} received for ${user}</echo>
     </target>

This example uses the 'unset' and 'post' tasks along with 'grep' to list the download url's for Ant:

        <target name="test">
          <unset name="ant_download_page"/>
          <post to="http://ant.apache.org/bindownload.cgi"
                verbose="no"
                property="ant_download_page"/>
          <grep in="${ant_download_page}"
                regex="select name=&quot;Preferred.*?&lt;/select"
                dotall="yes"
                property="options"/>

          <unset name="urls"/>
          <grep in="${options}"
             regex="&lt;option.*?&gt;(.*?)&lt;/option&gt;"
             group="1"
             allmatches="yes"
             separator="${line.separator}"
             property="urls"/>
          <echo>${urls}</echo>
        </target>

         [echo] http://apache.gr-linux.com
         [echo] http://www.reverse.net/pub/apache
         [echo] http://government-grants.org/mirrors/apache.org
         [echo] http://apache.mirrors.hoobly.com
         [echo] http://apache.mirrormax.net
         [echo] http://www.ibiblio.org/pub/mirrors/apache
         [echo] http://www.mirrormonster.com/apache.org
         [echo] http://apache.towardex.com
         [echo] http://www.axint.net/apache
         [echo] http://apache.tradebit.com/pub
         [echo] http://www.eng.lsu.edu/mirrors/apache
         [echo] http://mirrors.isc.org/pub/apache
         [echo] http://www.theshell.com/pub/apache
         [echo] http://apache.mirrors.redwire.net
         [echo] http://apache.cs.utah.edu
         [echo] http://www.tux.org/pub/net/apache/dist
         [echo] http://linux.cs.lewisu.edu/apache
         [echo] http://apache.roweboat.net
         [echo] http://apache.secsup.org/dist
         [echo] http://www.signal42.com/mirrors/apache
         [echo] http://apache.mirror99.com
         [echo] http://mirrors.xtria.com/apache
         [echo] http://apache.downlod.in
         [echo] http://apache.mirrors.pair.com
         [echo] http://apache.seekmeup.com
         [echo] http://mirrors.combose.com/apache
         [echo] http://www.wmwweb.com/apache
         [echo] http://apache.intissite.com
         [echo] http://apache.oregonstate.edu
         [echo] http://apache.bestwebcover.com
         [echo] http://ftp.wayne.edu/apache
         [echo] http://mirrors.ccs.neu.edu/Apache/dist
         [echo] http://www.ip97.com/apache.org
         [echo] http://apache.mirrors.versehost.com
         [echo] http://mirrors.playboy.com/apache
         [echo] ftp://ftp.ccs.neu.edu/net/mirrors/Apache/dist
         [echo] ftp://apache.mirrors.pair.com
         [echo] ftp://apache.cs.utah.edu/pub/apache.org
         [echo] ftp://apache.mirrors.redwire.net/pub/apache
         [echo] ftp://ftp.oregonstate.edu/pub/apache
         [echo] ftp://ftp.wayne.edu/apache
         [echo] ftp://mirror.sg.depaul.edu/pub/apache
         [echo] ftp://www.ibiblio.org/pub/mirrors/apache
         [echo] ftp://ftp.tux.org/pub/net/apache/dist
         [echo] ftp://www.reverse.net/pub/apache
         [echo] ftp://apache.secsup.org/pub/apache/dist
         [echo] http://www.eu.apache.org/dist (backup)
         [echo] http://www.apache.org/dist (backup)

Prev | Up | Next
Chapter 21. Call Task | Home | Chapter 23. Split Task