libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
regexConsole.cpp
/*
* A short example to demonstrate stream IO and regex pattern matching.
*/
#include <iostream>
#include <regex>
#include <z/core/string.hpp>
int main() {
std::regex regex;
zstring pattern;
while (!std::cin.eof()) {
// Get regex pattern to match against, exiting if user hits ctrl-D.
pattern.readln(std::cin);
if (std::cin.eof() && !pattern.length()) {
break;
}
// If pattern is invalid, print a short description of the error.
try {
regex.assign(pattern.str());
} catch (const std::regex_error &err) {
zstring(err.what()).writeln(std::cout);
continue;
}
// Prompt for the string to match to the regex pattern.
// Note that zstring is equivalent to z::core::string<z::utf8>
zstring(":").write(std::cout);
zstring text;
text.readln(std::cin);
// Show what substring the regex matched, if anything.
auto match = std::regex_search(text.str(), regex);
if (std::regex_search(text.str(), regex)) {
(text + " matches!").writeln(std::cout);
} else {
zstring("does not match.").writeln(std::cout);
}
}
return 0;
}
A template class for character strings.
Definition string.hpp:62
std::string str() const noexcept
Convert to a std::string.
Definition string.hpp:1592
int length() const noexcept override
Get the character count of the string.
string & readln(std::istream &stream) noexcept
Read string data from a stream until a newline is encountered.
void write(std::ostream &stream) const noexcept
Write string data to a stream in that stream's encoding.
Definition string.hpp:1496
void writeln(std::ostream &stream) const noexcept
Write string data to a stream in its format, appending a newline.
Definition string.hpp:1507