Выходные данные примера регулярного выражения C ++ отличаются от официальных - PullRequest
0 голосов
/ 04 ноября 2018

Следующий фрагмент кода взят из cppreference # regex :

#include <iostream>
#include <iterator>
#include <string>
#include <regex>

int main()
{
    std::string s = "Some people, when confronted with a problem, think "
        "\"I know, I'll use regular expressions.\" "
        "Now they have two problems.";

    std::regex self_regex("REGULAR EXPRESSIONS",
            std::regex_constants::ECMAScript | std::regex_constants::icase);
    if (std::regex_search(s, self_regex)) {
        std::cout << "Text contains the phrase 'regular expressions'\n";
    }

    std::regex word_regex("(\\S+)");
    auto words_begin = 
        std::sregex_iterator(s.begin(), s.end(), word_regex);
    auto words_end = std::sregex_iterator();

    std::cout << "Found "
              << std::distance(words_begin, words_end)
              << " words\n";

    const int N = 6;
    std::cout << "Words longer than " << N << " characters:\n";
    for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
        std::smatch match = *i;
        std::string match_str = match.str();
        if (match_str.size() > N) {
            std::cout << "  " << match_str << '\n';
        }
    }

    std::regex long_word_regex("(\\w{7,})");
    std::string new_s = std::regex_replace(s, long_word_regex, "[$&]");
    std::cout << new_s << '\n';
}

Хорошо компилируется, но вывод отличается от примера страницы, с которой я скопировал:

Text contains the phrase 'regular expressions'
Found 0 words
Words longer than 6 characters:
Some [people,] when [confronted] with a [problem,] think "I know, I'll use [regular] [expressions."] Now they have two [problems.]

Что должно быть:

Text contains the phrase 'regular expressions'
Found 19 words
Words longer than 6 characters:
  people,
  confronted
  problem,
  regular
  expressions."
  problems.
Some people, when [confronted] with a [problem], think 
"I know, I'll use [regular] [expressions]." Now they have two [problems].

Информация о моем ноутбуке:

Linux 4.15.0-33-generic # 36-Ubuntu SMP Ср. 15 августа 16:00:05 UTC 2018 x86_64 x86_64 x86_64 GNU / Linux

Информация о моем компиляторе:

clang version 6.0.0-1ubuntu2 (tags / RELEASE_600 / final) Цель: x86_64-pc-linux-gnu Модель резьбы: posix

Что вызывает разницу? версия компилятора?

Что я должен сделать, чтобы он работал правильно?

...