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> & 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 #include 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 namespace veregex = boost; #else #include namespace veregex = std; #endif #include #include #include #include #include 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(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> & 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