Click here to Skip to main content
12,882,639 members (29,504 online)
Click here to Skip to main content


63 bookmarked
Posted 27 Jun 2003

Henry Spencer's Regexp Engine Revisited

, 2 Jul 2003
A small, Unicode-aware regular expression engine based on Henry Spencer's early work
#ifndef STD_REGEXP_HPP__
#define STD_REGEXP_HPP__

#include <string>
#include <stdexcept>
#include <vector>

#include "regexp.h"

class regular_expression_error : public std::runtime_error
	regular_expression_error(int error_code, regexp* re);
	~regular_expression_error() throw() {};

	int code() const;
	const char* message() const;

	int code_;
	std::string message_;

class regular_expression
	typedef wchar_t CharT;
	typedef std::wstring string_type;
	typedef char CharT;
	typedef std::string string_type;

	typedef string_type::size_type size_type;
	typedef string_type::const_iterator const_iterator;

	regular_expression(const CharT* pattern);
	regular_expression(const string_type& pattern);

	bool exec(const CharT* match);
	bool exec(const string_type& match);

	bool matched(size_type sub_exp = 0) const;
	const_iterator begin(size_type sub_exp = 0) const;
	const_iterator end(size_type sub_exp = 0) const;
	string_type operator[] (size_type sub_exp) const;

	size_type size() const;

	class regexp_wrapper

		regexp* get();
		regexp** operator&();

		regexp* wrapped_;

		regexp_wrapper(const regexp_wrapper&);
		regexp_wrapper& operator=(const regexp_wrapper&);
	regexp_wrapper compiled_;
	string_type match_;
	std::vector<regmatch> matches_;

	// internal utilities
	void common_init(const CharT* pattern);
	void reset();
	// disable copying
	regular_expression(const regular_expression&);
	regular_expression& operator=(const regular_expression&);

#endif /* STD_REGEXP_HPP__ */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Canada Canada
I'm a senior software developer, working at Silanis Technology ( I've acquired quite a bit of experience (usually the hard way!) in Win32 and raw COM programming on the job. In my spare time, I like to monkey around with POSIX code.

I'm mostly interested in portable C++ libraries. I'm happiest when I develop portable C++ code--C++ being such a powerful language as long as one keeps clear of the rather nasty subtleties of the language.

I hope the articles I contribute will be of some help to someone. If even one person gains a few hours through use of that code, I'll be very happy.

When not coding, I like to listen to Anime and try to learn Japanese. It's not working too well so far, unfortunately. :{)

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 3 Jul 2003
Article Copyright 2003 by Sunspot
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid