libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
regex.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <regex>
12#include <promeki/core/string.h>
14
16
34class RegEx {
35 public:
37 using Flag = std::regex_constants::syntax_option_type;
38
40 static constexpr Flag IgnoreCase = std::regex::icase;
41
43 static constexpr Flag NoSubs = std::regex::nosubs;
44
46 static constexpr Flag Optimize = std::regex::optimize;
47
49 static constexpr Flag Collate = std::regex::collate;
50
52 static constexpr Flag ECMAScript = std::regex::ECMAScript;
53
55 static constexpr Flag Basic = std::regex::basic;
56
58 static constexpr Flag Extended = std::regex::extended;
59
61 static constexpr Flag Awk = std::regex::awk;
62
64 static constexpr Flag Grep = std::regex::grep;
65
67 static constexpr Flag EGrep = std::regex::egrep;
68
70 static constexpr Flag DefaultFlags = ECMAScript | Optimize;
71
77 RegEx(const String &pattern, Flag flags = DefaultFlags) : d(pattern.cstr(), flags), p(pattern) {}
78
84 RegEx(const char *pattern, Flag flags = DefaultFlags) : d(pattern, flags), p(pattern) {}
85
92 d = pattern.cstr();
93 p = pattern;
94 return *this;
95 }
96
101 String pattern() const {
102 return p;
103 }
104
110 bool match(const String &str) const {
111 std::smatch m;
112 return std::regex_match(str.str(), m, d);
113 }
114
120 bool search(const String &str) const {
121 return std::regex_search(str.str(), d);
122 }
123
129 StringList matches(const String& str) const {
131 std::smatch match;
132 const std::string &s = str.str();
133 auto pos = s.cbegin();
134 while(std::regex_search(pos, s.cend(), match, d)) {
135 matches += match.str();
136 pos = match.suffix().first;
137 }
138 return matches;
139 }
140
141 private:
142 std::regex d;
143 String p;
144};
145
147
Dynamic array container wrapping std::vector.
Definition list.h:40
ConstIterator cend() const noexcept
Returns a const iterator to one past the last element.
Definition list.h:208
ConstIterator cbegin() const noexcept
Returns a const iterator to the first element.
Definition list.h:168
Regular expression wrapper around std::regex.
Definition regex.h:34
std::regex_constants::syntax_option_type Flag
Syntax option flags for controlling regex behavior.
Definition regex.h:37
static constexpr Flag Awk
Use the awk POSIX regular expression grammar.
Definition regex.h:61
static constexpr Flag EGrep
Use the egrep (grep -E) POSIX regular expression grammar.
Definition regex.h:67
bool search(const String &str) const
Searches for the first occurrence of the pattern within the string.
Definition regex.h:120
static constexpr Flag ECMAScript
Use the Modified ECMAScript regular expression grammar.
Definition regex.h:52
RegEx(const char *pattern, Flag flags=DefaultFlags)
Constructs a RegEx from a C string pattern.
Definition regex.h:84
static constexpr Flag Extended
Use the extended POSIX regular expression grammar.
Definition regex.h:58
String pattern() const
Returns the current pattern string.
Definition regex.h:101
bool match(const String &str) const
Tests whether the entire string matches the pattern.
Definition regex.h:110
static constexpr Flag IgnoreCase
Case-insensitive matching.
Definition regex.h:40
RegEx & operator=(const String &pattern)
Assigns a new pattern to this regex.
Definition regex.h:91
StringList matches(const String &str) const
Returns all non-overlapping matches of the pattern in the string.
Definition regex.h:129
RegEx(const String &pattern, Flag flags=DefaultFlags)
Constructs a RegEx from a String pattern.
Definition regex.h:77
static constexpr Flag Optimize
Optimize the regex for faster matching at the cost of slower construction.
Definition regex.h:46
static constexpr Flag Collate
Make character ranges like "[a-b]" locale sensitive.
Definition regex.h:49
static constexpr Flag Basic
Use the basic POSIX regular expression grammar.
Definition regex.h:55
static constexpr Flag NoSubs
Treat all sub-expressions as non-marking; no matches are stored.
Definition regex.h:43
static constexpr Flag Grep
Use the grep POSIX regular expression grammar.
Definition regex.h:64
static constexpr Flag DefaultFlags
Default flags: ECMAScript grammar with optimization enabled.
Definition regex.h:70
Manages a list of strings.
Definition stringlist.h:21
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
const char * cstr() const
Returns a null-terminated C string pointer.
Definition string.h:289
const std::string & str() const
Returns a const reference to the underlying std::string.
Definition string.h:286
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19