1
/*************************************************************************************************
3
* Copyright (C) 2009-2012 FAL Labs
4
* This file is part of Kyoto Cabinet.
5
* This program is free software: you can redistribute it and/or modify it under the terms of
6
* the GNU General Public License as published by the Free Software Foundation, either version
7
* 3 of the License, or any later version.
8
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
* See the GNU General Public License for more details.
11
* You should have received a copy of the GNU General Public License along with this program.
12
* If not, see <http://www.gnu.org/licenses/>.
13
*************************************************************************************************/
16
#ifndef _KCREGEX_H // duplication check
22
namespace kyotocabinet { // common namespace
34
IGNCASE = 1 << 0, ///< case-insensitive
35
MATCHONLY = 1 << 1, ///< matching only
38
* Default constructor.
46
* Compile a string of regular expression.
47
* @param regex the string of regular expression.
48
* @param opts the optional features by bitwise-or: Regex::IGNCASE for case-insensitive
49
* matching, Regex::MATCHONLY for matching only usage.
51
bool compile(const std::string& regex, uint32_t opts = 0);
53
* Check whether a string matches the regular expression.
54
* @param str the string.
55
* @return true if the string matches, or false if not.
57
bool match(const std::string& str);
59
* Check whether a string matches the regular expression.
60
* @param str the string.
61
* @param alt the alternative string with which each substring is replaced. Each "$" in the
62
* string escapes the following character. Special escapes "$1" through "$9" refer to partial
63
* substrings corresponding to sub-expressions in the regular expression. "$0" and "$&" refer
64
* to the whole matching substring.
65
* @return the result string.
67
std::string replace(const std::string& str, const std::string& alt);
69
* Check whether a string matches a regular expression.
70
* @param str the string.
71
* @param pattern the matching pattern.
72
* @param opts the optional features by bitwise-or: Regex::IGNCASE for case-insensitive
73
* matching, Regex::MATCHONLY for matching only usage.
74
* @return true if the string matches, or false if not.
76
static bool match(const std::string& str, const std::string& pattern, uint32_t opts = 0) {
78
if (!regex.compile(pattern, opts)) return false;
79
return regex.match(str);
82
* Check whether a string matches the regular expression.
83
* @param str the string.
84
* @param pattern the matching pattern.
85
* @param alt the alternative string with which each substring is replaced. Each "$" in the
86
* string escapes the following character. Special escapes "$1" through "$9" refer to partial
87
* substrings corresponding to sub-expressions in the regular expression. "$0" and "$&" refer
88
* to the whole matching substring.
89
* @param opts the optional features by bitwise-or: Regex::IGNCASE for case-insensitive
90
* matching, Regex::MATCHONLY for matching only usage.
91
* @return the result string.
93
static std::string replace(const std::string& str, const std::string& pattern,
94
const std::string& alt, uint32_t opts = 0) {
96
if (!regex.compile(pattern, opts)) return str;
97
return regex.replace(str, alt);
100
/** Dummy constructor to forbid the use. */
102
/** Dummy Operator to forbid the use. */
103
Regex& operator =(const Regex&);
104
/** Opaque pointer. */
109
} // common namespace
111
#endif // duplication check