Code Monkey home page Code Monkey logo

compile-time-verbal-expressions's Introduction

Compile-time VerbalExpressions

C++ Regular Expressions made easy

CTVE is a C++17 library that helps construct difficult regular expressions. The regular expressions are build at compile time and can be used with compile time or runtime regular expression libraries.

It is based on the ideas of https://github.com/VerbalExpressions and powered by the wish to use Hana Dusíkovás fantastic compile time regular expression library without having to maintain code sprinkled with dozens of hard to read regular expressions.

Try online: https://godbolt.org/z/9j1x75

Example

#include <ctve.hpp>
#include <ctre.hpp>
#include <iostream>

namespace patterns
{
using namespace ctve;

static constexpr auto url =
    "http"
    + maybe('s')
    + "://"
    + maybe("www.")
    + capture(something_not_in(' ', '/', ':'))
    + maybe(':' + capture(digit.one_or_more()))
    + maybe(capture('/' + anything_not_in(' ')));
}

int main()
{
  auto&& [match, host, port, path] 
      = ctre::match<patterns::url>("https://github.com/mrpi/compile-time-verbal-expressions");
  if (match)
  {
    std::cout << "host: " << std::string_view{host} << '\n';
    std::cout << "port: " << std::string_view{port} << '\n';
    std::cout << "path: " << std::string_view{path} << '\n';
  }
}

Syntax

using namespace ctve; expected in following exampels

Code Matches PCRE
find('x') exactly the given character (?:x)
find("xyz") exactly the given character sequence (?:xzy)
'x' exactly the given character
(handled like find('x') by operator+)
(?:x)
"xyz" exactly the given character sequence
(handled like find("xyz") by operator+)
(?:xzy)
maybe('.') empty string or a single dot (?:\.)?
maybe(any_char) empty string or one char (?:.)?
digit a single digit [\d]
digit.one_or_more() one or more digits (?:[\d])+
digit.zero_or_more() zero or more digits (?:[\d])*
word_char.at_least(2) two or more word chars (?:[\w]){2,}
word_char.at_most(3) up to three word chars (?:[\w]){0,3}
whitespace.count(2,3) two or three whitespace chars (?:[\s]){2,3}
in(digit, range{'a','f'}) a hexadecimal digit [\da-f]
anything_not_in(' ', '/') string without spaces and slashes [^ /]*
something_not_in(digit, '.') non empty string without digits and dots [^\d\\.]+
capture(word_char.count(3)) capture three word chars ((?:[\w]){3})
"Hello" + maybe(whitespace) "Hello" optionally followed by a whitespace (?:Hello)(?:[\s])?
find("Hello") || "Hallo" "Hello" or "Hallo" (?:Hello)|(?:Hallo)

compile-time-verbal-expressions's People

Contributors

mrpi avatar wolfgangdvelop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

compile-time-verbal-expressions's Issues

Examples do not compile with MSVC2022 and C++20

Hi,
I use the newest CTRE 3.7.2 (but also tried a 2.x version with same results) and MSVC2022.

With C++17 activated, I can compile the examples, with C++20 activated, I get a compile error.

'ctll::fixed_string ctll::fixed_string(ctll::fixed_string)': could not deduce template argument for 'ctll::fixed_string' from 'const ctve::pattern<11>'

The problem seems to be, that you cannot construct a ctll::fixed_string out of a ctve::pattern or the underlying ctve::basic_string, as there is no constructor for std::array<char> in ctll::fixed_string.

I could not figure out, why it compiles in c++17. I guess some option is set in this mode, but I do not know which one.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.