(C++) BoostRegexExample1
January 17, 2018 · View on GitHub



Boost.Regex example 1 is an example how to use the Boost.Regex library.
The example below shows how to define a regular expression for a Dutch zip code, how to check for it and how to search for it.
- Download the Qt Creator project 'CppBoostRegexExample1' (zip)
Download the 'CppBoostRegexExample1' Windows executable (zip)
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppBoostRegexExample1/CppBoostRegexExample1.pro
include(../../ConsoleApplication.pri) #Or use the code below # QT += core # QT += gui # greaterThan(QT_MAJOR_VERSION, 4): QT += widgets # CONFIG += console # CONFIG -= app_bundle # TEMPLATE = app # CONFIG(release, debug|release) { # DEFINES += NDEBUG NTRACE_BILDERBIKKEL # } # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ # unix { # QMAKE_CXXFLAGS += -Werror # } include(../../Libraries/BoostAll.pri) SOURCES += main.cpp
./CppBoostRegexExample1/main.cpp
#include <cassert> #include <string> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #include <boost/regex.hpp> #pragma GCC diagnostic pop int main() { //Define how a dutch zip code is formatted const boost::regex dutch_zip_code("\\d{4}\\s[A-Z]{2}"); //Check if the regex works properly assert(boost::regex_match("1234 AB",dutch_zip_code)==true); assert(boost::regex_match("1234 ab",dutch_zip_code)==false); assert(boost::regex_match("1234ab",dutch_zip_code)==false); //Define a sentence with a Dutch zip code in it const std::string s = "My Dutch zip code is 1234 AB."; //Show how boost::regex_match and boost::regex_search work assert(boost::regex_match(s,dutch_zip_code)==false && "the std::string does not match a dutch zip code"); assert(boost::regex_search(s,dutch_zip_code)==true && "but the std::string does contain a dutch zip code"); //Show how to obtain a Dutch zip code from a std::string const boost::sregex_iterator i(s.begin(),s.end(),dutch_zip_code); const std::string t = i->str(); assert(t=="1234 AB"); }
./CppBoostRegexExample1/CppBoostRegexExample1.sh
#!/bin/bash #Script to check the status of compiles #Copies executables (both Linux and Win32) executables to ~/bin (overwrites older) #set -x verbose #echo on mytempfile="tmp.txt" if [ -e $mytempfile ] then rm $mytempfile fi rm *.pro.user for myprofile in `ls | egrep ".pro\>"` do echo $myprofile mybasename=`echo $myprofile | sed "s/\.pro//"` #For every .pro file, # 0: compile # 1: crosscompile using Qt5 for type in 0 1 do myqmake="" mytypestr="" #Cleaning up rm Makefile rm Makefile.* rm -r release rm -r debug rm ui_*.h rm qrc_*.cpp rm moc_*.cpp rm object_script*.* rm *.o rm *_plugin_import.cpp case $type in 0) myqmake="qmake" mytypestr="Lubuntu" ;; 1) myqmake="../../Libraries/mxe/usr/i686-pc-mingw32/qt5/bin/qmake" mytypestr="Qt5LubuntuToWindows" ;; esac $myqmake $myprofile if [ ! -e Makefile ] then echo $myprofile", "$mytypestr": FAIL (Makefile not found)" >> ../$mytempfile continue fi make if [ -e $mybasename ] || [ -e ./release/$mybasename".exe" ] then echo $myprofile", "$mytypestr": SUCCESS" >> ../$mytempfile #echo "SUCCESS for mybasename: "$mybasename if [ -e $mybasename ] then #echo "(1) cp "$mybasename" ~/bin/" cp $mybasename ~/bin/ rm $mybasename fi if [ -e ./release/$mybasename".exe" ] then #echo "(2) cp ./release/"$mybasename".exe ~/bin/" cp ./release/$mybasename".exe" ~/bin/ fi else echo $myprofile", "$mytypestr": FAIL (executable not found)" >> ../$mytempfile #echo "FAIL for mybasename: "$mybasename fi #Cleaning up rm Makefile rm Makefile.* rm -r release rm -r debug rm ui_*.h rm qrc_*.cpp rm moc_*.cpp rm object_script*.* rm *.o rm *_plugin_import.cpp done #next type done #next profile cat $mytempfile rm $mytempfile