Regex error in C++ - c++

I have simple code which doesn't work. It throws this error: what(): regex_error and I have no idea why. Thanks for any help. I tried to compile it with this flag: -std=c++11.
#include <iostream>
#include <regex>
int main() {
std::regex r("[1-9]{4}");
std::cout << "result: " << std::regex_match("1524", r) << '\n';
return 0;
}

Related

missing output in the eclipse console when debugging

I recently started using Eclipse CDT (version 2019-03) with the Cygwin toolchain and have noticed some bizarre behaviour when using the debugger.
Under the debugger the following program behaves as you would expect
#include <iostream>
int main()
{
std::cout << "hello world\n" << std::flush;
}
However the following produces no output
#include <iostream>
int main()
{
std::cout << "* world\n" << std::flush;
}
And for the following the output is world
#include <iostream>
int main()
{
std::cout << "# world\n" << std::flush;
}
This behaviour is completely consistent and reproduceable. Does anyone have any explanation or workarounds?

ios_base::failure has no code()

I tried the code from cppreference.com but it seems e.code() is not there:
#include <iostream>
#include <system_error> // std::make_error_condition, std::ios_errc
int main () {
std::cin.exceptions (std::ios::failbit|std::ios::badbit);
try {
std::cin.rdbuf(nullptr); // throws
} catch (std::ios::failure& e) {
std::cerr << "Fehler: ";
if (e.code() == // <<< ERROR: no e.code() ???
std::make_error_condition(std::io_errc::stream))
std::cerr << "stream\n";
else
std::cerr << "other\n";
}
}
My g++-6.2 and g++-5 and clang-3.9 (on linux) all say the same:
error: ‘class std::ios_base::failure’ has no member named ‘code’
if (e.code() == std::make_error_condition(std::io_errc::stream))
^~~~
Even the unchanged example does not compile for me
#include <iostream>
#include <fstream>
int main()
{
std::ifstream f("doesn't exist");
try {
f.exceptions(f.failbit);
} catch (const std::ios_base::failure& e)
{
std::cout << "Caught an ios_base::failure.\n"
<< "Explanatory string: " << e.what() << '\n'
<< "Error code: " << e.code() << '\n';
}
}
with
$ g++-6 etest.cpp -o etest.x
etest.cpp: In function ‘int main()’:
etest.cpp:12:42: error: ‘const class std::ios_base::failure’ has no member named ‘code’
<< "Error code: " << e.code() << '\n';
I tried g++-6, g++-5, adding -std=c++1y, -std=c++98, same result. (the latter is ok, though, I believe).
Which is really odd, because the online compiler on the site does compile it.
The single hint I have from a run with -E (preprocessor):
class ios_base
{
# 246 "/usr/include/c++/6/bits/ios_base.h" 3
public:
# 276 "/usr/include/c++/6/bits/ios_base.h" 3
class failure : public exception
{
public:
This looks as if failure does indeed not derive from system_error, which would explain why there is no code(). But why? Its plain Ubuntu g++, its g++-6, its C++14... I have no compiler tweaks, links, hacks...
There seems to be some use of
#if _GLIBCXX_USE_CXX11_ABI
in the vicinity. But does that interfere? If so, how to do I make it un-interfere?
Does anyone have any idea what might be going on here?

Compiles but getting 'uncaught exception of type std::bad_cast'

I'm trying to get started with Boost for C++. Here's a small program that compiles with g++ -Wall test.cpp /usr/local/Cellar/boost/1.55.0/lib/libboost_locale-mt.a.
However, when I run it, here's the error I get:
libc++abi.dylib: terminating with uncaught exception of type std::bad_cast: std::bad_cast
Abort trap: 6
#include <string>
#include <iostream>
#include <boost/locale.hpp>
int main(void) {
char test[] = "Variété";
boost::locale::to_upper(test);
std::cout << test << std::endl;
return 0;
}
What could be the reason here? Thanks!
I'm on Mac OSX Mavericks.
According to docs:
http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/group__convert.html#ga7889a57e1bc1059fbb107db0781d0b6d
std::basic_string<CharType> boost::locale::to_lower(CharType const *str,
std::locale const &loc = std::locale())
Convert a NUL terminated string str to lower case according to locale loc
Note:
throws std::bad_cast if loc does not have converter facet installed
So, this fixes the problem on my machine.
#include <string>
#include <iostream>
#include <boost/locale.hpp>
int main(void) {
std::string test = "Variété";
std::locale loc = boost::locale::generator().generate("en_US.UTF-8");
std::string test_u = boost::locale::to_upper(test, loc);
std::cout << test << " -> " << test_u << std::endl;
return 0;
}
Outputs:
Variété -> VARIÉTÉ

Regex C Help on escape character

I am having trouble extracting the token values from my string : "JOIN #ROOM\r\n"
I am compiling my code on Mingw64 with the following arguments : g++ tregex.cpp -o tregex.exe -std=gnu++11
I get this error , but not my exception for some reason :
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
This is my code :
#include <regex>
#include <string>
#include <iostream>
using namespace std;
//Tregex.cpp
int main(void) {
regex rgx("[[:cntrl:]]");
string str = "JOIN #ROOM\r\n";
smatch match;
try{
if(regex_search(str, match, rgx))
for(auto token:match) cout << token <<"\n";
cout<< endl;
}
catch(regex_error & e){
if( e.code() == regex_constants::error_escape )
cerr << "invalid escape character \n";
else if( e.code() == regex_constants::error_stack )
cerr << "regular expression is not big enough\n";
else
cerr << "exception caught: "<< e.what()<<"\n";
}
cin.get();
return 0;
}
FWIW, there's nothing wrong with your C++ code, and it works perfectly using Clang and libc++ on my MacBook.
As indicated by the comments above, <regex> is one of the features that works in the LLVM project's libc++ but has never worked properly in the GNU project's libstdc++. You might try switching to libc++ if it's available for your platform.

abort() called using std::regex to validate URL

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main ()
{
if (std::regex_match ("http://www.google.com", std::regex("(http|https):\/\/(\w+\.)*(\w*)\/([\w\d]+\/{0,1})+")))
std::cout << "valid URL \n";
std::cout << std::endl;
return 0;
}
its compiling with the warnings ,but when I executed it gives
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted (core dumped)
what I should Do?
The warnings that you are ignoring are probably telling you what the problem is.
By looking at the pattern, you have not properly escaped the pattern string.
Properly escaping the pattern string to use '\' to escape the backslash will solve the problem. Otherwise, the compiler is tring to interpret the character that follows the un-escaped backslash into a string control character.
std::regex("(http|https)://(\\w+.)(\\w)/([\\w\\d]+/{0,1})+")
Try cpp-netlib:
#include <string>
#include <iostream>
#include <boost/network/uri.hpp>
int main (int argc, char ** argv)
{
std::string address = "http://www.google.com";
boost::network::uri::uri uri_(address);
if ( !boost::network::uri::valid(uri_) )
{
// error
std::cout << "not valid" << std::endl;
return 0;
}
std::cout << "valid" << std::endl;
std::string host = boost::network::uri::host(uri_);
std::string port = boost::network::uri::port(uri_);
std::string scheme = boost::network::uri::scheme(uri_);
return 0;
}
How to build (cpp-netlib is in /root/cpp-netlib-0.9.4/ in my case):
g++ main.cpp -L/root/cpp-netlib-0.9.4/libs/network/src/ -I/root/cpp-netlib-0.9.4/ -o main -lcppnetlib-uri -lboost_system