Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Boost Boost.Regex example 1

 

Boost.Regex example 1 is a 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.

 

 

 

 

 

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppRegexExample1.pro

 

QT       += core
QT       -= gui
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

unix {
  LIBS += -lboost_regex
}

win32 {
  #Boost.Regex
  HEADERS += \
    ../../Libraries/boost_1_53_0/libs/regex/src/internals.hpp

  SOURCES += \
    ../../Libraries/boost_1_53_0/libs/regex/src/winstances.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/wide_posix_api.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/wc_regex_traits.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/w32_regex_traits.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/usinstances.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/static_mutex.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/regex_traits_defaults.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/regex_raw_buffer.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/regex_debug.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/regex.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/posix_api.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/instances.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/icu.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/fileiter.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/c_regex_traits.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/cregex.cpp \
    ../../Libraries/boost_1_53_0/libs/regex/src/cpp_regex_traits.cpp
}

 

 

 

 

 

main.cpp

 

#include <cassert>
#include <string>
#include <boost/regex.hpp>

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");
}

 

 

 

 

 

crosscompiletowindows.sh

 

#!/bin/sh
#From http://richelbilderbeek.nl/CppQtCrosscompileToWindowsExample15.htm

echo "Cross compiling to Windows"

echo "1/2: Creating Windows makefile"
i686-pc-mingw32-qmake CppRegexExample1.pro

echo "2/2: making makefile"

make

echo "Done"

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml