(C++) BoostRegexExample2

January 17, 2018 · View on GitHub

BoostQt
CreatorLubuntu

Boost.Regex example 2: replace is an example how to use the Boost.Regex library.

The example below shows how to perform a replacement within a regular expression.

Technical facts

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppBoostRegexExample2/CppBoostRegexExample2.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

 

 

 

 

 

./CppBoostRegexExample2/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() {   const std::string str = "The zip code 1234AB is correct";   const boost::regex regex("(\\d{4})()([A-Z]{2})");   const std::string fix("(\$1) (\$3)");   const std::string s     = boost::regex_replace(str, regex, fix,         boost::match_default | boost::format_all);   assert(s == "The zip code 1234 AB is correct"); }

 

 

 

 

 

./CppBoostRegexExample2/CppBoostRegexExample2.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