firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.28k stars 199 forks source link

C++ code generator #1467

Open TheEpicFace007 opened 3 years ago

TheEpicFace007 commented 3 years ago

Code Generator Language

C++

Code Snippet

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

int main()
{
    std::string sre = "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 regex("(\\w+)");
    auto words_begin = 
        std::sregex_iterator(s.begin(), s.end(), 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';
}

This generator take in account the regex replacement and the regex searching. It's take from cpp reference(https://en.cppreference.com/w/cpp/regex)

Doqnach commented 3 years ago

Thank you for this example code.

Please see https://github.com/firasdib/Regex101/wiki/Writing-a-Code-Generator on how to help us implement this code generator.

FYI: I believe that std::regex in c++ only does POSIX? Would it be worth to have an example using something like Boost or similar very common library that actually supports PCRE? Since the website supports PCRE and not POSIX.

TheEpicFace007 commented 3 years ago

std regex in c++ can also do ecma script js varient(if you supply the std::regex_constants::ECMAScript in the regex constructor) and work in widnows. It work fine under msvc.

i'll follow up with what you guys need to add the generator

TheEpicFace007 commented 3 years ago

Non-global match:

I think you need to do this

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';
        }
    }

global match:

according to this stack overflow answer you use std::regex_search

non global subtitution

    std::string new_s = std::regex_replace(s, long_word_regex, "[$&]");

global subsitutiion

std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");

multiline matching

codeproject.com/Questions/1221494/Simple-multiline-regex-in-Cplusplus

note:

you must use ecmascript option to make regexes that are like js regex so you can recycle the current js engine for the c++ regexp