返回字符串中模式的所有非重叠匹配

3 c++ python regex pattern-matching

在 Python 中,我可以使用 re.findall(pattern, string) 返回字符串中模式的所有非重叠匹配项。

例如,在以下 SVG 路径命令中:

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']
Run Code Online (Sandbox Code Playgroud)

在 C 或 C++ 中,有没有一种轻量级、干净且有效的方法来进行这种类型的正则表达式模式匹配?请注意,我不是在寻找依赖 Boost 的解决方案。理想情况下,我想最大限度地减少依赖关系并保持我的代码精益...

Joh*_*ooy 5

SLRE - 超轻正则​​表达式库

SLRE是一个 ANSI C 库,它实现了 Perl 正则表达式的一个很小的子集。它主要面向想要解析配置文件的开发人员,速度并不重要。它在单个 .c 文件中,可根据自定义需要轻松修改。例如,如果要引入一个新的元字符“\i”,意思是“IP 地址”,这样做很容易。特征

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

支持的 RE 语法

^ 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.
 * ----------------------------------------------------------------------------
 */
Run Code Online (Sandbox Code Playgroud)