views:

109

answers:

2

In Python I can use re.findall(pattern, string) to return all non-overlapping matches of pattern in a string.

For example, in the following SVG path command:

import re
spam = "M317.0,169.7C311.1,170.5 285.7,146.8 300.7,178.57 L 321.4,175.01"
eggs = re.findall("([A-Za-z]|-?[0-9]+\.?[0-9]*(?:e-?[0-9]*)?)", spam)
print(eggs)
['M', '317.0', '169.7', 'C', '311.1', '170.5', '285.7', '146.8', '300.7', '178.5', 'L', '321.4', '175.0']

This there a light-weight, clean, and efficient way to do this type of regular expression pattern matching in in C or C++? Please note that I'm not looking for a solution that relies upon Boost. Ideally I would like to minimize dependencies and keep my code lean...

+2  A: 

You'll need a C++ regular expression library. There are a number of them but they will create a dependency. C++ has no native (or STL) regular expression support.

Pace
+4  A: 

SLRE - Super Light Regular Expression library

SLRE is an ANSI C library that implements a tiny subset of Perl regular expressions. It is primarily targeted for developers who want to parse configuation files, where speed is unimportant. It is in single .c file, easily modifiable for custom needs. For example, if one wants to introduce a new metacharacter, '\i', that means 'IP address', it is easy to do so. Features

* Crossplatform - pure ANSI C
* Very simple API
* Light: about 5kB of code when compiled
* Uses no dynamic memory allocation
* Thread safe

Supported RE Syntax

^ Match beginning of a buffer
$ Match end of a buffer
() Grouping and substring capturing
[...] Match any character from set
[^...] Match any character but ones from set
\s Match whitespace
\S Match non-whitespace
\d Match decimal digit
\r Match carriage return
\n Match newline
+ Match one or more times (greedy)
+? Match one or more times (non-greedy)
* Match zero or more times (greedy)
*? Match zero or more times (non-greedy)
? Match zero or once
\xDD Match byte with hex value 0xDD
\meta Match one of the meta character: ^$().[*+?\

/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * Sergey Lyubka wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.
 * ----------------------------------------------------------------------------
 */
gnibbler
+1 for the license text :-)
frunsi