Add util-regexp module.
[tairon.git] / src / util-regexp / regexp.h
blob01aa5d62bc51e4ca2320fa8fc2a87d08c6d7794a
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation and appearing *
8 * in the file LICENSE.LGPL included in the packaging of this file. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Library General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _tairon_util_regexp_regexp_h
18 #define _tairon_util_regexp_regexp_h
20 #include <regex.h>
22 #include <tairon/core/exceptions.h>
24 using Tairon::Core::String;
26 namespace Tairon
29 namespace Util
32 /** \brief This class provides pattern matching using POSIX regular
33 * expressions.
35 * Regular expressions provide a way to to find patterns withing text. It can
36 * be useful in situations like validating, searching, searching and replacing
37 * and string splitting.
39 class RegExp
41 public:
42 /** Constructs a regular expression object.
44 * \param pattern Pattern that will be used for matching. See regex(7)
45 * for details.
47 RegExp(const char *pattern);
49 /** Destroys the object and frees all allocated resources.
51 ~RegExp();
53 /** Attempts to find a match within a string. Returns position of the
54 * first match, or -1 if there is no match.
56 int match(const String &s);
58 /** Returns the text matched by the nth subexpression. The entire match
59 * has index 0 and the parenthesized subexpressions have indices
60 * starting from 1 (excluding non-capturing parentheses).
62 String matched(int nth = 0) const;
64 /** Returns length of the nth matched subexpression, or -1 if there is
65 * no such matched subexpression.
67 unsigned int matchedLength(int nth) const;
69 /** Returns starting position of the nth matched subexpression.
71 unsigned int matchedStart(int nth) const;
73 private:
74 /** Size of the match buffer.
76 int count;
78 /** Buffer for matches.
80 regmatch_t *pmatch;
82 /** Pattern buffer storage area.
84 regex_t preg;
86 /** String that is searched for a match.
88 String text;
92 /** \brief Exception that is used for RegExp errors.
94 class RegExpException : public Tairon::Core::Exception
96 public:
97 /** Standard constructor.
99 RegExpException(const String &desc) : Tairon::Core::Exception(desc) {};
101 /** Default destructor.
103 ~RegExpException() {};
106 }; // namespace Util
108 }; // namespace Tairon
110 #endif
112 // vim: ai sw=4 ts=4 noet fdm=marker