3
#include <regex>
#include <iostream>

using namespace std;

void out(bool b)
{
    cout<< ( b ? "found" : "not found" )<<endl;
}

int main()
{

    // find XML/HTML-tagged value(tags before and after the value must match):
    //regex reg2("<(.*)>.*</\\1>");

    regex reg2(R"(<(.*)>.*</\1>)");
    bool found = regex_match("<tag>value</tag>",
            reg2);
    out(found);
}

$ g++ -g -std=c++11 regex1.cpp

$ ./a.out

Segmentation fault (core dumped)


$ gdb a.out core.12473 

GNU gdb (GDB) Fedora (7.6-30.fc19)
Reading symbols from /home/neo/code/regex/a.out...done.
[New LWP 12473]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x0804a352 in std::__detail::_StateSeq::_M_append (this=0xbf948a30, __rhs=...)
    at /usr/include/c++/4.8.1/bits/regex_nfa.tcc:157
157       _M_nfa[_M_end2]._M_next = __rhs._M_start;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-11.fc19.i686 libgcc-4.8.1-1.fc19.i686 libstdc++-4.8.1-1.fc19.i686
(gdb) bt
#0  0x0804a352 in std::__detail::_StateSeq::_M_append (this=0xbf948a30, __rhs=...)
    at /usr/include/c++/4.8.1/bits/regex_nfa.tcc:157
#1  0x0804ea33 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:779
#2  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#3  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#4  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#5  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#6  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#7  0x0804ea01 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_alternative (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:776
#8  0x0804dc21 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_M_disjunction (
    this=0xbf948c68) at /usr/include/c++/4.8.1/bits/regex_compiler.h:758
#9  0x0804ce38 in std::__detail::_Compiler<char const*, std::regex_traits<char> >::_Compiler (this=0xbf948c68, 
    __b=@0xbf948d34: 0x80538ff "<(.*)>.*</\\1>", __e=@0xbf948d0c: 0x805390c "", __traits=..., __flags=16)
    at /usr/include/c++/4.8.1/bits/regex_compiler.h:729
#10 0x0804bb51 in std::__detail::__compile<char const*, std::regex_traits<char> > (
    __b=@0xbf948d34: 0x80538ff "<(.*)>.*</\\1>", __e=@0xbf948d0c: 0x805390c "", __t=..., __f=16)
    at /usr/include/c++/4.8.1/bits/regex_compiler.h:1105
#11 0x0804b0bb in std::basic_regex<char, std::regex_traits<char> >::basic_regex (this=0xbf948d4c, 
    __p=0x80538ff "<(.*)>.*</\\1>", __f=16) at /usr/include/c++/4.8.1/bits/regex.h:388
#12 0x08049847 in main () at regex1.cpp:17
linquize
  • 19,828
  • 10
  • 59
  • 83
Prem
  • 49
  • 4
  • 3
    GCC doesn't support ``. – Rapptz Aug 15 '13 at 06:11
  • 1
    As Rapptz mentioned, gcc doesn't support `` yet; [here](http://stackoverflow.com/questions/12530406/is-gcc4-7-buggy-about-regular-expressions/12665408#12665408) are the details of why it doesn't, if you're interested. – Praetorian Aug 15 '13 at 06:17
  • @Praetorian GCC4.8 supports regex. #include #include using namespace std; void out(bool b) { cout<< ( b ? "found" : "not found" )<.*"); bool found = regex_match("value", reg1); out(found); } $ g++ -g -std=c++11 regex1.cpp $ ./a.out found $ – Prem Aug 15 '13 at 06:34
  • Thanks @falsetru regex reg2(R"(<(.*)>.*\\1>)"); // works regex reg2("<(.*)>.*\\1>"); // doesn't work Do you know why? – Prem Aug 15 '13 at 06:49
  • I don't know about raw string in C++. – falsetru Aug 15 '13 at 06:53
  • regex reg2("<(.*)>.*\\1>"); works in Visual C++ 2010 – Prem Aug 15 '13 at 08:29
  • @Prem Umm, no it doesn't. Did you read the answer I linked to? As it explains, the header exists, but support is minimal at best, and often broken. [Here's](http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011) the status of libstdc++, it clearly shows `regex` is not implemented. – Praetorian Aug 15 '13 at 14:58
  • Actually, work is currently being done, and more parts of the `` header could be available in GCC 4.9. – Morwenn Aug 15 '13 at 17:25

1 Answers1

0

C++11 regular expression is not fully supported in GCC until 4.9 (current trunk as of this writing). For details check http://gcc.gnu.org/gcc-4.9/changes.html, under section "Runtime Library (libstdc++)".

manphiz
  • 191
  • 3
  • 8