master 7ee381d495e1 cached
6 files
13.2 KB
3.5k tokens
40 symbols
1 requests
Download .txt
Repository: VerbalExpressions/CppVerbalExpressions
Branch: master
Commit: 7ee381d495e1
Files: 6
Total size: 13.2 KB

Directory structure:
gitextract_7am2qwis/

├── .gitignore
├── CMakeLists.txt
├── LICENSE
├── README.md
├── example.cpp
└── verbalexpressions.hpp

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
*.o
*.obj
example
CMakeFiles
cmake_install.cmake
CMakeCache.txt
CppVerbalExpressionsExample
Makefile


================================================
FILE: CMakeLists.txt
================================================
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.7)
PROJECT(CppVerbalExpressions)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -std=c++11 -Wall -Wextra -Weffc++ -pedantic")
IF(IS_DIRECTORY /opt/boxen)
	SET(BOOST_ROOT /opt/boxen/homebrew)
ENDIF()
FIND_PACKAGE(Boost 1.46.0 REQUIRED COMPONENTS regex)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIRS})
ADD_EXECUTABLE(CppVerbalExpressionsExample example.cpp)
TARGET_LINK_LIBRARIES(CppVerbalExpressionsExample ${Boost_LIBRARIES})


================================================
FILE: LICENSE
================================================
The MIT License (MIT)

Copyright (c) 2013 whackashoe

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


================================================
FILE: README.md
================================================
CppVerbalExpressions
====================


## C++ Regular Expressions made easy
VerbalExpressions is a C++11 Header library that helps to construct difficult regular expressions.

This C++ lib is based off of the (original) Javascript [VerbalExpressions](https://github.com/jehna/VerbalExpressions) library by [jehna](https://github.com/jehna/).

## Other Implementations
You can see an up to date list of all ports on [VerbalExpressions.github.io](http://VerbalExpressions.github.io).
- [Javascript](https://github.com/jehna/VerbalExpressions)
- [Ruby](https://github.com/VerbalExpressions/RubyVerbalExpressions)
- [C#](https://github.com/VerbalExpressions/CSharpVerbalExpressions)
- [Python](https://github.com/VerbalExpressions/PythonVerbalExpressions)
- [Java](https://github.com/VerbalExpressions/JavaVerbalExpressions)
- [PHP](https://github.com/VerbalExpressions/PHPVerbalExpressions)

## How to get started

In case you do not have C++11 compliant standard library you can still use boost.regex.

## Examples

Here's a couple of simple examples to give an idea of how VerbalExpressions works:

### Testing if we have a valid URL

```c++
// Create an example of how to test for correctly formed URLs
verex expr = verex()
            .search_one_line()
            .start_of_line()
            .then( "http" )
            .maybe( "s" )
            .then( "://" )
            .maybe( "www." )
            .anything_but( " " )
            .end_of_line();

// Use verex's test() function to find if it matches
std::cout << expr.test("https://www.google.com") << std::endl;

// Ouputs the actual expression used: ^(?:http)(?:s)?(?:://)(?:www.)?(?:[^ ]*)$
std::cout << expr << std::endl;
```

### Replacing strings

```c++
// Create a test string
std::string replaceMe = "Replace bird with a duck";
// Create an expression that seeks for word "bird"
verex expr2 = verex().find("bird");
// Execute the expression
std::cout << expr2.replace(replaceMe, "duck") << std::endl;
```

### Shorthand for string replace:

```c++
std::cout << verex().find( "red" ).replace( "We have a red house", "blue" ) << std::endl;
```




Here you can find the API documentation for Verbal Expressions

## Basic usage
Basic usage of Verbal Expressions starts from the expression `verex()`. You can chain methods afterwards. Those are described under the "terms" section.

```c++
auto expr = verex();
```

## API

### Terms
* .anything()
* .anything_but( const std::string & value )
* .something()
* .something_but(const std::string & value)
* .end_of_line()
* .find( const std::string & value )
* .maybe( const std::string & value )
* .start_of_line()
* .then( const std::string & value )

### Special characters and groups
* .any( const std::string & value )
* .any_of( const std::string & value )
* .br()
* .linebreak()
* .range( const std::vector<std::pair<std::string, std::string>> & args )
* .range( const std::std::string & a, const & std::string b )
* .tab()
* .word()

### Modifiers
* .with_any_case()
* .search_one_line()
* .search_global()

### Functions
* .replace( const std::string & source, const std::string & value )
* .test()

### Other
* .add( expression )
* .multiple( const std::string & value )
* .alt()


================================================
FILE: example.cpp
================================================
#include "verbalexpressions.hpp"
#include <iostream>
#include <string>

int main() {
    using verex::verex;

    // Create an example of how to test for correctly formed URLs
    verex expr = verex()
                .search_one_line()
                .start_of_line()
                .then( "http" )
                .maybe( "s" )
                .then( "://" )
                .maybe( "www." )
                .anything_but( " " )
                .end_of_line();

    // Use VerEx's test() function to find if it matches
    std::cout << (expr.test("https://www.google.com") ? "matches" : "doesn't match") << std::endl;

    // Ouputs the actual expression used
    std::cout << expr << std::endl;


    // Create a test string
    std::string replaceMe = "Replace bird with a duck";
    // Create an expression that seeks for word "bird"
    verex expr2 = verex().find("bird");
    // Execute the expression
    std::cout << expr2.replace(replaceMe, "duck") << std::endl;

    // Shorthand string replace
    std::cout << verex().find( "red" ).replace( "We have a red house", "blue" ) << std::endl;
    return 0;
}


================================================
FILE: verbalexpressions.hpp
================================================
/*!
 * VerbalExpressions C++ Library v0.1
 * https://github.com/whackashoe/C++VerbalExpressions
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 whackashoe
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef VERBAL_EXPRESSIONS_H_
#define VERBAL_EXPRESSIONS_H_


#ifdef USE_BOOST
#include <boost/regex.hpp>
namespace veregex = boost;
#else
#include <regex>
namespace veregex = std;
#endif

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>

namespace verex
{

class verex
{
using flag_type = veregex::regex::flag_type;

private:
    std::string prefixes;
    std::string source;
    std::string suffixes;
    std::string pattern;
    unsigned int modifiers;


    enum Flags {
        GLOBAL          = 1
      , MULTILINE       = 2
      , CASEINSENSITIVE = 4
    };

    friend std::ostream& operator<<(std::ostream &strm, verex &v)
    {
        return strm << v.pattern;
    }

    flag_type check_flags() const
    {
        return (modifiers & CASEINSENSITIVE) ? veregex::regex::icase : static_cast<flag_type>(0);
    }

    std::string reduce_lines(const std::string & value) const
    {
        const std::size_t pos = value.find("\n");

        if(pos == std::string::npos) {
            return value;
        }

        return value.substr(0, pos);
    }

public:
    verex() :
          prefixes("")
        , source("")
        , suffixes("")
        , pattern("")
        , modifiers(0) {};

    verex& operator=(const verex& ve) = default;
    ~verex() = default;

    verex & add(const std::string & value)
    {
        source = source + value;
        pattern = prefixes + source + suffixes;
        return (*this);
    }

    verex & start_of_line(const bool enable)
    {
        prefixes = enable ? "^" : "";
        return add("");
    }

    verex & start_of_line()
    {
        return start_of_line(true);
    }

    verex & end_of_line(const bool enable)
    {
        suffixes = enable ? "$" : "";
        return add("");
    }

    verex & end_of_line()
    {
        return end_of_line(true);
    }

    verex & then(const std::string & value)
    {
        return add("(?:" + value + ")");
    }

    verex & find(const std::string & value)
    {
        return then(value);
    }

    verex & maybe(const std::string & value)
    {
        return add("(?:" + value + ")?");
    }

    verex & anything()
    {
        return add("(?:.*)");
    }

    verex & anything_but(const std::string & value)
    {
        return add("(?:[^" + value + "]*)");
    }

    verex & something()
    {
        return add("(?:.+)");
    }

    verex & something_but(const std::string & value)
    {
        return add("(?:[^" + value + "]+)");
    }

    std::string replace(const std::string & source, const std::string & value)
    {
        return veregex::regex_replace(
            source
          , veregex::regex(pattern, check_flags())
          , value
        );
    }

    verex & linebreak()
    {
        return add("(?:(?:\\n)|(?:\\r\\n))");
    }

    verex & br()
    {
        return linebreak();
    }

    verex & tab()
    {
        return add("\\t");
    }

    verex & word()
    {
        return add("\\w+");
    }

    verex & any_of(const std::string & value)
    {
        return add( "[" + value + "]" );
    }

    verex & any(const std::string & value)
    {
        return any_of(value);
    }

    verex & range(const std::vector<std::pair<std::string, std::string>> & args)
    {
        std::stringstream value;
        value << "[";

        for (const auto & item : args) {
            const std::string from = item.first;
            const std::string to   = item.second;

            value << from << "-" << to;
        }

        value << "]";

        return add(value.str());
    }

    verex & range(const std::string & a, const std::string & b)
    {
        return range({{a, b}});
    }

    verex & add_modifier(const char modifier)
    {
        switch (modifier) {
            case 'i':
                modifiers |= CASEINSENSITIVE;
                break;
            case 'm':
                modifiers |= MULTILINE;
                break;
            case 'g':
                modifiers |= GLOBAL;
                break;
            default:
                break;
        }

        return (*this);
    }

    verex & remove_modifier(const char modifier)
    {
        switch (modifier) {
            case 'i':
                modifiers ^= CASEINSENSITIVE;
                break;
            case 'm':
                modifiers ^= MULTILINE;
                break;
            case 'g':
                modifiers ^= GLOBAL;
                break;
            default:
                break;
        }

        return (*this);
    }


    verex & with_any_case(const bool enable)
    {
        if (enable) {
            add_modifier('i');
        } else {
            remove_modifier('i');
        }

        return (*this);
    }

    verex & with_any_case()
    {
        return with_any_case(true);
    }

    verex & search_one_line(const bool enable)
    {
        if (enable) {
            remove_modifier('m');
        } else {
            add_modifier('m');
        }

        return (*this);
    }

    verex & search_one_line()
    {
        return search_one_line(true);
    }

    verex & search_global(const bool enable)
    {
        if (enable) {
            add_modifier('g');
        } else {
            remove_modifier('g');
        }

        return (*this);
    }

    verex & search_global()
    {
        return search_global(true);
    }

    verex & multiple(const std::string & value)
    {
        if (value.length() > 0 && value.at(0) != '*' && value.at(0) != '+') {
            add("+");
        }

        return add(value);
    }

    verex & alt(const std::string & value)
    {
        if (prefixes.find("(") == std::string::npos) {
            prefixes += "(";
        }

        if (suffixes.find(")") == std::string::npos) {
            suffixes = ")" + suffixes;
        }

        add(")|(");

        return then(value);
    }

    bool test(const std::string & value)
    {
        const std::string to_test = (modifiers & MULTILINE) ? value : reduce_lines(value);

        if (modifiers & GLOBAL) {
            return veregex::regex_search(to_test, veregex::regex(pattern, check_flags()));
        } else {
            return veregex::regex_match(to_test, veregex::regex(pattern, check_flags()));
        }
    }
};

} // namespace verex

#endif
Download .txt
gitextract_7am2qwis/

├── .gitignore
├── CMakeLists.txt
├── LICENSE
├── README.md
├── example.cpp
└── verbalexpressions.hpp
Download .txt
SYMBOL INDEX (40 symbols across 2 files)

FILE: example.cpp
  function main (line 5) | int main() {

FILE: verbalexpressions.hpp
  type verex (line 45) | namespace verex
    class verex (line 48) | class verex
      type Flags (line 60) | enum Flags {
      method flag_type (line 71) | flag_type check_flags() const
      method reduce_lines (line 76) | std::string reduce_lines(const std::string & value) const
      method verex (line 88) | verex() :
      method verex (line 95) | verex& operator=(const verex& ve) = default;
      method verex (line 98) | verex & add(const std::string & value)
      method verex (line 105) | verex & start_of_line(const bool enable)
      method verex (line 111) | verex & start_of_line()
      method verex (line 116) | verex & end_of_line(const bool enable)
      method verex (line 122) | verex & end_of_line()
      method verex (line 127) | verex & then(const std::string & value)
      method verex (line 132) | verex & find(const std::string & value)
      method verex (line 137) | verex & maybe(const std::string & value)
      method verex (line 142) | verex & anything()
      method verex (line 147) | verex & anything_but(const std::string & value)
      method verex (line 152) | verex & something()
      method verex (line 157) | verex & something_but(const std::string & value)
      method replace (line 162) | std::string replace(const std::string & source, const std::string & ...
      method verex (line 171) | verex & linebreak()
      method verex (line 176) | verex & br()
      method verex (line 181) | verex & tab()
      method verex (line 186) | verex & word()
      method verex (line 191) | verex & any_of(const std::string & value)
      method verex (line 196) | verex & any(const std::string & value)
      method verex (line 201) | verex & range(const std::vector<std::pair<std::string, std::string>>...
      method verex (line 218) | verex & range(const std::string & a, const std::string & b)
      method verex (line 223) | verex & add_modifier(const char modifier)
      method verex (line 242) | verex & remove_modifier(const char modifier)
      method verex (line 262) | verex & with_any_case(const bool enable)
      method verex (line 273) | verex & with_any_case()
      method verex (line 278) | verex & search_one_line(const bool enable)
      method verex (line 289) | verex & search_one_line()
      method verex (line 294) | verex & search_global(const bool enable)
      method verex (line 305) | verex & search_global()
      method verex (line 310) | verex & multiple(const std::string & value)
      method verex (line 319) | verex & alt(const std::string & value)
      method test (line 334) | bool test(const std::string & value)
Condensed preview — 6 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (14K chars).
[
  {
    "path": ".gitignore",
    "chars": 101,
    "preview": "*.o\n*.obj\nexample\nCMakeFiles\ncmake_install.cmake\nCMakeCache.txt\nCppVerbalExpressionsExample\nMakefile\n"
  },
  {
    "path": "CMakeLists.txt",
    "chars": 483,
    "preview": "CMAKE_MINIMUM_REQUIRED(VERSION 2.8.7)\nPROJECT(CppVerbalExpressions)\n\nSET(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -O3 -std=c+"
  },
  {
    "path": "LICENSE",
    "chars": 1077,
    "preview": "The MIT License (MIT)\n\nCopyright (c) 2013 whackashoe\n\nPermission is hereby granted, free of charge, to any person obtain"
  },
  {
    "path": "README.md",
    "chars": 3206,
    "preview": "CppVerbalExpressions\n====================\n\n\n## C++ Regular Expressions made easy\nVerbalExpressions is a C++11 Header lib"
  },
  {
    "path": "example.cpp",
    "chars": 1117,
    "preview": "#include \"verbalexpressions.hpp\"\n#include <iostream>\n#include <string>\n\nint main() {\n    using verex::verex;\n\n    // Cre"
  },
  {
    "path": "verbalexpressions.hpp",
    "chars": 7533,
    "preview": "/*!\n * VerbalExpressions C++ Library v0.1\n * https://github.com/whackashoe/C++VerbalExpressions\n *\n * The MIT License (M"
  }
]

About this extraction

This page contains the full source code of the VerbalExpressions/CppVerbalExpressions GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 6 files (13.2 KB), approximately 3.5k tokens, and a symbol index with 40 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!