最近我需要把propkeys.h(在C[++]中)翻译成C#。
我的目标是将以下内容转换为C#:
DEFINE_PROPERTYKEY(PKEY_Audio_ChannelCount, 0x64440490, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03, 7);
致:
public static PropertyKey Audio_ChannelCount = new PropertyKey(new Guid("{64440490-4C8B-11D1-8B70-080036B11A03}"));
我使用Notepad++进行正则表达式匹配,但是我也可以接受其他可脚本化的解决方案(如perl,sed)。请不要使用编译型语言(如C#,Java...)。
最终我得到了这个(有效):
// TURNS GUID into String
// Find what (Line breaks inserted for convenience):
0x([[:xdigit:]]{8}),\s*0x([[:xdigit:]]{4}),\s*0x([[:xdigit:]]
{4}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]
{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]
{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]{2})
// Replace with:
new Guid\("{$1-$2-$3-$4$5-$6$7$8$9$10$11}"\)
// Final pass
// Find what:
^DEFINE_PROPERTYKEY\(PKEY_(\w+),\s*(new Guid\("\{[[:xdigit:]|\-]+"\)),\s*\d+\);$
// Replace with:
public static PropertyKey $1 = new PropertyKey\($2\);
虽然这个方法可行,但我觉得第一遍的结果有点奇怪。我想用一个重复的{1}来代替成堆的{2}。
(0x([[:xdigit:]]){2},\s*)+
但无法将其与群组一起使用。有人能告诉我如何使用正则表达式以“标准”方式完成此操作吗?