C++ Regex

Seppi123

Erfahrenes Mitglied
Irgendwie will das bei mir nicht funktionieren, angeblich ist es nicht in der Klasse std:: aber auf einer sehr guten Tutorial Seite habe ich das 1:1 übernommen. Es geht um das Thema Regex ;)
Ich möchte nur einen kleinen Converter von PHP zu C++ (ja ich weiß ihr findet es unnötig bzw. seht den Sinn nicht darin ABER es soll ein privates , not published Programm werden, was einem Freund ein wenig das programmieren erleichtern soll (PHP'ler) und mir helfen soll, mit so einem Mammut-Projekt die Sprache noch besser näher zu bringen :) - RegEx ist bei jeder Sprache, die ich kann ein Problem, was ich habe (ich kanns einfach nicht zu blöd ^^)

Code:
#include <iostream>
#include <string>
#include <regex>
 
int main()
{
    // Simple regular expression matching
    std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
    std::regex txt_regex("[a-z]+\\.txt");
 
    for (const auto &fname : fnames) {
        std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
    }   
 
    // Extraction of a sub-match
    std::regex base_regex("([a-z]+)\\.txt");
    std::smatch base_match;
 
    for (const auto &fname : fnames) {
        if (std::regex_match(fname, base_match, base_regex)) {
            // The first sub_match is the whole string; the next
            // sub_match is the first parenthesized expression.
            if (base_match.size() == 2) {
                std::ssub_match base_sub_match = base_match[1];
                std::string base = base_sub_match.str();
                std::cout << fname << " has a base of " << base << '\n';
            }
        }
    }
 
    // Extraction of several sub-matches
    std::regex pieces_regex("([a-z]+)\\.([a-z]+)");
    std::smatch pieces_match;
 
    for (const auto &fname : fnames) {
        if (std::regex_match(fname, pieces_match, pieces_regex)) {
            std::cout << fname << '\n';
            for (size_t i = 0; i < pieces_match.size(); ++i) {
                std::ssub_match sub_match = pieces_match[i];
                std::string piece = sub_match.str();
                std::cout << "  submatch " << i << ": " << piece << '\n';
            }   
        }   
    }   
}

Als Fehlermeldung kommt folgendes (Benutze Code::Blocks mit dem Compiler GNU GCC Compiler):
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|
C:\PHC++\Converter\main.cpp||In function 'int main()':|
C:\PHC++\Converter\main.cpp|9|error: 'regex' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|9|error: expected ';' before 'txt_regex'|
C:\PHC++\Converter\main.cpp|11|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
C:\PHC++\Converter\main.cpp|11|error: ISO C++ forbids declaration of 'fname' with no type [-fpermissive]|
C:\PHC++\Converter\main.cpp|11|error: range-based 'for' loops are not allowed in C++98 mode|
C:\PHC++\Converter\main.cpp|12|error: 'regex_match' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|12|error: 'txt_regex' was not declared in this scope|
C:\PHC++\Converter\main.cpp|16|error: 'regex' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|16|error: expected ';' before 'base_regex'|
C:\PHC++\Converter\main.cpp|17|error: 'smatch' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|17|error: expected ';' before 'base_match'|
C:\PHC++\Converter\main.cpp|19|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
C:\PHC++\Converter\main.cpp|19|error: ISO C++ forbids declaration of 'fname' with no type [-fpermissive]|
C:\PHC++\Converter\main.cpp|19|error: range-based 'for' loops are not allowed in C++98 mode|
C:\PHC++\Converter\main.cpp|20|error: 'regex_match' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|20|error: 'base_match' was not declared in this scope|
C:\PHC++\Converter\main.cpp|20|error: 'base_regex' was not declared in this scope|
C:\PHC++\Converter\main.cpp|24|error: 'ssub_match' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|24|error: expected ';' before 'base_sub_match'|
C:\PHC++\Converter\main.cpp|25|error: 'base_sub_match' was not declared in this scope|
C:\PHC++\Converter\main.cpp|32|error: 'regex' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|32|error: expected ';' before 'pieces_regex'|
C:\PHC++\Converter\main.cpp|33|error: 'smatch' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|33|error: expected ';' before 'pieces_match'|
C:\PHC++\Converter\main.cpp|35|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
C:\PHC++\Converter\main.cpp|35|error: ISO C++ forbids declaration of 'fname' with no type [-fpermissive]|
C:\PHC++\Converter\main.cpp|35|error: range-based 'for' loops are not allowed in C++98 mode|
C:\PHC++\Converter\main.cpp|36|error: 'regex_match' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|36|error: 'pieces_match' was not declared in this scope|
C:\PHC++\Converter\main.cpp|36|error: 'pieces_regex' was not declared in this scope|
C:\PHC++\Converter\main.cpp|39|error: 'ssub_match' is not a member of 'std'|
C:\PHC++\Converter\main.cpp|39|error: expected ';' before 'sub_match'|
C:\PHC++\Converter\main.cpp|40|error: 'sub_match' was not declared in this scope|
||=== Build finished: 31 errors, 3 warnings (0 minutes, 0 seconds) ===|

Dann noch eine Frage, wie kann ich jeglichen Text filtern? Ich erinnere mich irgendwas mit (*.?) (bei PHP) und bei C++ war es soweit ich das nun noch behalten hatte [*.?] oder?
 
Naja die Fehlermeldung sagt ja schon was das Problem ist:

#error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options

<- Einfach eine der oben genanten Parameter an der Compiler/Linker geben. Musst wahrscheinlich in Projektoptionen angeben.
 
Passt jetzt nicht direkt zu deiner Frage, aber mit regulären Ausdrücken kannst du keinen Transcompiler schreiben! Du brauchst mindestens einen Parser und einen Lexer.
 
Zurück