在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']
这是一种轻量级,干净且高效的方法,可以在C或C ++中进行这种类型的正则表达式模式匹配吗?请注意,我不是在寻找依赖Boost的解决方案。理想情况下,我希望最大限度地减少依赖关系,并保持我的代码精益......
答案 0 :(得分:4)
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.
* ----------------------------------------------------------------------------
*/
答案 1 :(得分:2)
您需要一个C ++正则表达式库。它们中有很多但它们会产生依赖性。 C ++没有本机(或STL)正则表达式支持。