Glob pattern to regex translator in C++11. Optionally, directory traversal with glob pattern in C++17. Header-only library.

GlobToRegex

  • Glob pattern to regex translator in C++11.
  • Directory traversal with glob pattern in C++17.

GlobToRegex supports the following glob notations:

  • ?
  • *
  • **
  • [a-z], [!a-z]

String matching test with glob patterns (C++11)

specimens = { "at", "bat", "cat", "tab", "tac", }; const std::string globPattern = "?at"; const std::string regexStr = GlobToRegex::translateGlobPatternToRegex(globPattern); if(regexStr.empty()) { printf("error\n"); exit(EXIT_FAILURE); } const std::regex::flag_type regexFlags = std::regex::ECMAScript; const std::regex r = std::regex(regexStr, regexFlags); for(const auto& s : specimens) { const bool b = std::regex_match(s, r); printf("'%s' %-13s '%s'\n", globPattern.c_str(), b ? "matches" : "doesn't match", s.c_str()); } return 0; } ">
#include <stdio.h>
#include "glob_to_regex.hpp"

int main() {
    const std::vector
    specimens = { 
   "at", 
   "bat", 
   "cat", 
   "tab", 
   "tac", };

    
   const std::string globPattern = 
   "?at";

    
   const std::string regexStr = 
   GlobToRegex::translateGlobPatternToRegex(globPattern);
    
   if(regexStr.
   empty()) {
        
   printf(
   "error\n");
        
   exit(EXIT_FAILURE);
    }

    
   const std::regex::flag_type regexFlags = std::regex::ECMAScript;
    
   const std::regex r = 
   std::regex(regexStr, regexFlags);

    
   for(
   const 
   auto& s : specimens) {
        
   const 
   bool b = 
   std::regex_match(s, r);
        
   printf(
   "'%s' %-13s '%s'\n", globPattern.
   c_str(), b ? 
   "matches" : 
   "doesn't match", s.
   c_str());
    }

    
   return 
   0;
}
  

Directory traversal with glob pattern matching (C++17)

bool { printf(" %s\n", (const char*) path.generic_u8string().c_str()); return true; } ); } ">
#include <stdio.h>
#include "glob_to_regex.hpp"

int main(int argc, const char** argv) {
#if _WIN32
    const bool caseSensitivity = false;
    const auto home = std::string(getenv_("USERPROFILE"));
#else
    const bool caseSensitivity = true;
    const auto home = std::string(getenv("HOME"));
#endif
    const bool followSimlink = true;

    std::filesystem::path globPattern = home + "/**/*.txt";
    printf("globPattern=%s\n", (const char*) globPattern.generic_u8string().c_str());

    GlobToRegex::dirWalk(
          caseSensitivity
        , followSimlink
        , globPattern.generic_u8string().c_str()
        , [&](const std::filesystem::path& path) -> bool {
            printf("  %s\n", (const char*) path.generic_u8string().c_str());
            return true;
        }
    );
}
Owner
Similar Resources

A BOF to parse the imports of a provided PE-file, optionally extracting symbols on a per-dll basis.

PE Import Enumerator BOF What is this? This is a BOF to enumerate DLL files to-be-loaded by a given PE file. Depending on the number of arguments, thi

Dec 1, 2022

Reliable & unreliable messages over UDP. Robust message fragmentation & reassembly. P2P networking / NAT traversal. Encryption.

GameNetworkingSockets GameNetworkingSockets is a basic transport layer for games. The features are: Connection-oriented API (like TCP) ... but message

Dec 30, 2022

Glob for C++17

Glob for C++17

Unix-style pathname pattern expansion Table of Contents Quick Start Build Library and Standalone Sample Usage API Wildcards Examples Match file extens

Jan 4, 2023

My attempt at implementing the fast voxel traversal algorithm by Amanatides and Woo using OpenGL

Attempt at implementing the fast voxel traversal algorithm Works on both Linux and Windows. Recommended software: Linux: Visual Studio Code / Windows:

Sep 24, 2021

Raven is like a simplified SSH with NAT traversal.

Raven Raven works like a simplified SSH with NAT traversal. Now developing... But you can still have a test for fun! Just fill server_ip in Raven.conf

Oct 26, 2022

The approximate regex matching library and agrep command line tool.

Introduction TRE is a lightweight, robust, and efficient POSIX compliant regexp matching library with some exciting features such as approximate (fuzz

Dec 26, 2022

A non-backtracking NFA/DFA-based Perl-compatible regex engine matching on large data streams

Name libsregex - A non-backtracking NFA/DFA-based Perl-compatible regex engine library for matching on large data streams Table of Contents Name Statu

Dec 22, 2022

Easier CPP interface to PCRE regex engine with global match and replace

RegExp Easier CPP interface to PCRE regex engine with global match and replace. I was looking around for better regex engine than regcomp for my C/C++

May 21, 2022

match(it): A lightweight header-only pattern-matching library for C++17 with macro-free APIs.

match(it): A lightweight header-only pattern-matching library for C++17 with macro-free APIs. Features Easy to get started. Single header library. Mac

Dec 27, 2022

ServiceLocator - Service Locator Pattern Header-Only Library

Service Locator Very fast, header-only C++ Service Locator Pattern library What is the Service Locator Pattern The Service Locator Pattern is a design

Feb 21, 2022

Simple header only pattern matching for c++14

Simple, Extensible C++ Pattern Matching Library I have recently been looking at Haskell and Rust. One of the things I wanted in C++ from those languag

Dec 30, 2022

Intel 8080 CP/M 2.2 to Intel 8086/8088 MS-DOS assembly language translator.

Intel 8080 CP/M 2.2 to Intel 8086/8088 MS-DOS assembly language translator.

8088ify 8088ify is an Intel 8080 CP/M 2.2 to Intel 8086 (8088) MS-DOS assembly language translator. This means that 8088ify reads in assembly language

Dec 23, 2022

exp2swift translator which translates STEP schema files described in ISO 10303-11 STEP EXPRESS language to the Swift programing language.

STEPswiftcode/ exp2swift exp2swift translator which translates STEP schema files described in ISO 10303-11 STEP EXPRESS language to the Swift programi

Jan 3, 2022

HLSL Parser and Translator for HLSL, GLSL, and MSL.

HLSLParser This is a fork of Unknownworld's hlslparser adapted to our needs in The Witness. We currently use it to translate pseudo-HLSL shaders (usin

Jul 2, 2022

HLSL to GLSL language translator based on ATI's HLSL2GLSL. Used in Unity.

HLSL to GLSL shader language translator ⚠️ As of mid-2016, the project is unlikely to have any significant developments. At Unity we are moving to a d

Dec 18, 2022

HLSL Parser and Translator for HLSL, GLSL, and MSL.

HLSLParser This is a fork of Unknownworld's hlslparser adapted to our needs in The Witness. We currently use it to translate pseudo-HLSL shaders (usin

Dec 25, 2022

Experimental OpenCL SPIR-V to OpenCL C translator

spirv2clc spirv2clc is an experimental OpenCL SPIR-V to OpenCL C translator currently targeting OpenCL 1.2 support. It can generate OpenCL C code equi

Oct 1, 2022

Repo for the DSA Mini-project Directory Manager

Directory Manager Repo for the DSA Mini-project Directory Manager Installation and Setup To setup the program on your desktop, do the following. git c

Oct 16, 2021
The approximate regex matching library and agrep command line tool.

Introduction TRE is a lightweight, robust, and efficient POSIX compliant regexp matching library with some exciting features such as approximate (fuzz

Dec 26, 2022
A non-backtracking NFA/DFA-based Perl-compatible regex engine matching on large data streams

Name libsregex - A non-backtracking NFA/DFA-based Perl-compatible regex engine library for matching on large data streams Table of Contents Name Statu

Dec 22, 2022
Easier CPP interface to PCRE regex engine with global match and replace

RegExp Easier CPP interface to PCRE regex engine with global match and replace. I was looking around for better regex engine than regcomp for my C/C++

May 21, 2022
High-performance regular expression matching library

Hyperscan Hyperscan is a high-performance multiple regex matching library. It follows the regular expression syntax of the commonly-used libpcre libra

Jan 1, 2023
regular expression library

Oniguruma https://github.com/kkos/oniguruma Oniguruma is a modern and flexible regular expressions library. It encompasses features from different reg

Jan 3, 2023
Perl Incompatible Regular Expressions library

This is PIRE, Perl Incompatible Regular Expressions library. This library is aimed at checking a huge amount of text against relatively many regular

Oct 9, 2022
RE2 is a fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, and Python. It is a C++ library.

This is the source code repository for RE2, a regular expression library. For documentation about how to install and use RE2, visit https://github.co

Jan 4, 2023
Onigmo is a regular expressions library forked from Oniguruma.

Onigmo (Oniguruma-mod) https://github.com/k-takata/Onigmo Onigmo is a regular expressions library forked from Oniguruma. It focuses to support new exp

Dec 6, 2022
SRL-CPP is a Simple Regex Language builder library written in C++11 that provides an easy to use interface for constructing both simple and complex regex expressions.

SRL-CPP SRL-CPP is a Simple Regex Language builder library written in C++11 that provides an easy to use interface for constructing both simple and co

Mar 9, 2022