【发布时间】:2025-12-15 11:35:01
【问题描述】:
我需要一些帮助来构建匹配以下字符串格式的正则表达式:
typedef enum x
{
...
} z;
我想在哪里获得 x 和 z 的值,即使它们没有被指定。
谢谢!
【问题讨论】:
-
如果没有指定,你将如何获得价值?
标签: python regex parsing match findall
我需要一些帮助来构建匹配以下字符串格式的正则表达式:
typedef enum x
{
...
} z;
我想在哪里获得 x 和 z 的值,即使它们没有被指定。
谢谢!
【问题讨论】:
标签: python regex parsing match findall
import re
str_typedef = """typedef enum x
{
...
} z;"""
pattern = 'typedef\s+enum\s+([a-zA-Z0-9_]+)\s*{[^{}]*}\s*([a-zA-Z0-9_]+)\s*;'
rs = re.findall(pattern, str_typedef)
for r in rs:
enum_type_name = r.group(1)
enum_name = r.group(2)
# do operations with results here, or store to an array
# of dictionaries for use later.
这里,enum_type_name 最终会变成“x”或替代 x 的任何其他名称,而 enum_name 最终会变成“z”或替代 z 的任何其他名称。
表达式的快速总结:
每个\s+ 是一个或多个空格或换行符,每个\s* 都是相同的,但空格是可选的。
每个([a-zA-Z0-9_]+) 将捕获并存储一组一个或多个字母数字字符,用于 c++ 变量/类/枚举名称。
{[^{}]*} 表示{ 后跟任意数量的不是{ 或} 的字符,然后是}。
【讨论】: