30 const std::string &pattern = args[1].asString();
31 const std::string &text = args[2].asString();
33 std::regex re(pattern);
34 bool match = std::regex_match(text, re);
37 catch (
const std::regex_error &e)
39 throw std::runtime_error(
"Regex error in regex_match: " + std::string(e.what()));
53 const std::string &pattern = args[1].asString();
54 std::string text = args[2].asString();
58 size_t end_pos = text.length();
60 if (args.size() > 3 && !args[3].isNull())
62 start_pos =
static_cast<size_t>(args[3].asInt());
65 if (args.size() > 4 && !args[4].isNull())
67 end_pos =
static_cast<size_t>(args[4].asInt());
70 if (start_pos > end_pos || end_pos > text.length())
72 throw std::out_of_range(
"Invalid start/end positions in regex_search");
75 std::regex re(pattern);
77 std::string search_text = text.substr(start_pos, end_pos - start_pos);
79 bool found = std::regex_search(search_text, match, re);
82 return found ?
Value(match[0].str()) :
Value(
"");
84 catch (
const std::regex_error &e)
86 throw std::runtime_error(
"Regex error in regex_search: " + std::string(e.what()));
88 catch (
const std::out_of_range &e)
90 throw std::runtime_error(
"Position out of range in regex_search: " + std::string(e.what()));
102 const std::string &pattern = args[1].asString();
103 const std::string &text = args[2].asString();
105 std::regex re(pattern);
106 std::sregex_iterator it(text.begin(), text.end(), re);
107 std::sregex_iterator end;
118 match.
setField(
"position",
Value(
static_cast<int64_t
>(it->position())));
121 result.
setField(std::to_string(count++), match);
126 result.
setField(
"count",
Value(
static_cast<int64_t
>(count)));
130 catch (
const std::regex_error &e)
132 throw std::runtime_error(
"Regex error in regex_findall: " + std::string(e.what()));
145 const std::string &pattern = args[1].asString();
146 const std::string &text = args[2].asString();
149 if (args.size() > 3 && !args[3].isNull())
151 max_split =
static_cast<int>(args[3].asInt());
154 std::regex re(pattern);
155 std::sregex_token_iterator it(text.begin(), text.end(), re, -1);
156 std::sregex_token_iterator end;
160 std::vector<std::string> parts;
163 while (it != end && (max_split == -1 || count < max_split))
165 parts.push_back(it->str());
171 if (it != end && max_split != -1)
173 std::string remaining = it->str();
176 remaining += it->str();
182 result.
setField(
"count",
Value(
static_cast<int64_t
>(count)));
186 catch (
const std::regex_error &e)
188 throw std::runtime_error(
"Regex error in regex_split: " + std::string(e.what()));
202 const std::string &pattern = args[1].asString();
203 std::string text = args[2].asString();
204 const std::string &replacement = args[3].asString();
207 std::regex_constants::syntax_option_type syntax = std::regex_constants::ECMAScript;
208 std::regex_constants::match_flag_type match_flags = std::regex_constants::match_default;
209 std::regex_constants::match_flag_type format_flags = std::regex_constants::match_default;
212 if (args.size() > 4 && !args[4].isNull())
214 const std::string &flagsStr = args[4].asString();
217 if (flagsStr.find(
'e') != std::string::npos)
219 syntax = std::regex_constants::ECMAScript;
221 if (flagsStr.find(
'a') != std::string::npos)
223 syntax = std::regex_constants::awk;
225 if (flagsStr.find(
'g') != std::string::npos)
227 syntax = std::regex_constants::grep;
229 if (flagsStr.find(
'p') != std::string::npos)
231 syntax = std::regex_constants::egrep;
234 if (flagsStr.find(
'i') != std::string::npos)
236 syntax = syntax | std::regex_constants::icase;
238#if (defined(__GNUC__) && __cplusplus >= 201703L) || (defined(_MSC_VER) && _MSC_VER >= 1950)
239 if (flagsStr.find(
'm') != std::string::npos)
241 syntax = syntax | std::regex_constants::multiline;
244 if (flagsStr.find(
'n') != std::string::npos)
246 syntax = syntax | std::regex_constants::nosubs;
248 if (flagsStr.find(
'o') != std::string::npos)
250 syntax = syntax | std::regex_constants::optimize;
252 if (flagsStr.find(
'c') != std::string::npos)
254 syntax = syntax | std::regex_constants::collate;
258 if (flagsStr.find(
'f') != std::string::npos)
260 format_flags = format_flags | std::regex_constants::format_sed;
262 if (flagsStr.find(
'r') != std::string::npos)
264 format_flags = format_flags | std::regex_constants::format_no_copy;
266 if (flagsStr.find(
'd') != std::string::npos)
268 format_flags = format_flags | std::regex_constants::format_first_only;
272 std::regex re(pattern, syntax);
274 auto combined_flags = match_flags | format_flags;
275 std::string result = std::regex_replace(text, re, replacement, combined_flags);
277 return Value(result);
279 catch (
const std::regex_error &e)
281 throw std::runtime_error(
"Regex error in regex_replace: " + std::string(e.what()));