Click here to Skip to main content
15,896,153 members
Articles / Desktop Programming / WTL

Henry Spencer's Regexp Engine Revisited

Rate me:
Please Sign up or sign in to vote.
4.88/5 (19 votes)
2 Jul 200317 min read 178.4K   3.4K   67  
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
{
public:
	regular_expression_error(int error_code, regexp* re);
	~regular_expression_error() throw() {};

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

private:
	int code_;
	std::string message_;
};

class regular_expression
{
public:
#ifdef REGEXP_UNICODE
	typedef wchar_t CharT;
	typedef std::wstring string_type;
#else
	typedef char CharT;
	typedef std::string string_type;
#endif

	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);
	~regular_expression();

	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;

private:
	class regexp_wrapper
	{
	public:
		regexp_wrapper();
		~regexp_wrapper();

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

	private:
		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.

License

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


Written By
Web Developer
Canada Canada
I'm a senior software developer, working at Silanis Technology (http://www.silanis.com). 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. :{)

Comments and Discussions