// NAnt - A .NET build tool // Copyright (C) 2001-2003 Gerry Shaw // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Arjen Poutsma (poutsma@yahoo.com) using System; using System.Collections; using System.Text.RegularExpressions; using SourceForge.NAnt; using SourceForge.NAnt.Attributes; namespace SourceForge.NAnt.Tasks { /// /// Sets project properties based on the evaluatuion of a regular expression. /// /// /// The "pattern" attribute must contain one or more /// named grouping constructs, /// which represents the names of the properties to be set. /// These named grouping constructs can be enclosed by angle brackets (?<name>) or single quotes (?'name'). /// In the build file, use the XML element to specify <, and to specify >. /// The named grouping construct must not contain any punctuation and it cannot begin with a number. /// /// /// Find the last word in the given string and stores it property lastword. /// /// /// /// ]]> /// /// Splits the full filename and extension of a filename. /// /// /// ]]> /// /// [TaskName("regex")] public class RegexTask : Task { private string _pattern = null; private string _input = null; /// Represents the regular expression to be evalued. /// Represents the regular expression to be evalued. /// /// The pattern must contain one or more named constructs, which may /// not contain any punctuation and cannot begin with a number. /// [TaskAttribute("pattern", Required=true)] public string Pattern { get { return _pattern;} set { _pattern = value; } } /// Represents the input for the regular expression. /// Represents the input for the regular expression. [TaskAttribute("input", Required=true)] public string Input { get { return _input;} set { _input = value; } } /// /// Executes the task. /// protected override void ExecuteTask() { Regex regex = new Regex(_pattern); Match match = regex.Match(_input); // we start the iteration at 1, since the collection of groups // always starts with a group which matches the entire input and is named '0' // this group is of no interest to us for (int i=1; i < match.Groups.Count; i++) { string groupName = regex.GroupNameFromNumber(i); //Log.WriteLine("Group:{0} Value:{1}", groupName, match.Groups[groupName].Value); Properties [ groupName ] = match.Groups[groupName].Value; } } } }