Cppregex is a very simple, single header file, easy to use C++11 std::regex wrapper for lazy people who just want to use regex in C++ without having to mingle with std::regex boilerplate code. In other words, a simple C++ regex library.
Of course, this requires C++11 to work.
Include cppregex.h
in your project and you are good to go.
This is #include "cppregex.h"
for those of us that like to copy & paste.
Examples for every function in the library are written in example.cpp
.
Cppregex includes the following functions:
-
bool regexMatch(string text, string regex)
returnstrue
iftext
matches theregex
,false
otherwise. -
bool regexContains(string text, string regex)
returnstrue
iftext
contains a substring that matchesregex
,false
otherwise. -
int regexIndex(string text, string regex)
returns the position of the first substring oftext
that matchesregex
,-1
if none was found. -
vector<int> regexIndices(string text, string regex)
returns a vector if ints containing all the positions of the substrings oftext
that matchregex
. An emptyvector<int>
is returned if none was found. -
pair<int, unsigned int> regexIndexLength(string text, string regex)
returns the position and length of the first substring oftext
that matchesregex
.<-1, 0>
is returned if none was found. -
vector<pair<int, unsigned int>> regexIndicesLengths(string text, string regex)
returns a vector of pairs of position and length of each substring oftext
that matchesregex
. An emptyvector<pair<int, unsigned int>>
is returned if none was found. -
string regexSearch(string text, string regex)
returns the first substring oftext
that matchesregex
,""
if none was found. -
vector<string> regexSearchAll(string text, string regex)
returns all the substrings oftext
that matchregex
, an emptyvector<string>
is returned if none was found. -
string regexBefore(string text, string regex)
returns all the text found intext
before a substring that matchedregex
was found. -
string regexAfter(string text, string regex)
returns all the text found intext
after a substring that matchedregex
was found. -
string regexReplace(string text, string regex, string newValue)
returns a copy oftext
with the first substring that matchedregex
replaced bynewValue
. -
string regexReplaceAll(string text, string regex, string newValue)
returns a copy oftext
with the every substring that matchedregex
replaced bynewValue
.
Cppregex is released under the MIT License so you are free to do whatever you want with it. Enjoy!