Regex 正则表达式模式重复和捕获

Regex 正则表达式模式重复和捕获,regex,notepad++,Regex,Notepad++,我最近不得不将propkeys.h(在C[++]中)翻译成C# 我的目标是来自: DEFINE_PROPERTYKEY(PKEY_Audio_ChannelCount, 0x64440490, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03, 7); 致: 我使用Notepad++编写正则表达式,但我对任何其他可编写脚本的解决方案(perl、sed…)都持开放态度。请不要使用编译语言(如C#,Java…) 我最终得到

我最近不得不将propkeys.h(在C[++]中)翻译成C#

我的目标是来自:

DEFINE_PROPERTYKEY(PKEY_Audio_ChannelCount, 0x64440490, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03, 7);
致:

我使用Notepad++编写正则表达式,但我对任何其他可编写脚本的解决方案(perl、sed…)都持开放态度。请不要使用编译语言(如C#,Java…)

我最终得到了这个(工作):

虽然这是工作,我觉得第一次通过一些奇怪的。我想用一个重复的来代替成吨的{2}。 比如:

(0x([[:xdigit:]]){2},\s*)+

但不能让它与团队合作。有人能告诉我一种使用正则表达式的“标准”方法吗?

不幸的是,当您使用量词执行匹配时,组将匹配整个文本,因此更“经典”的解决方案是使用与perl的\G元字符等效的方法,它在上一次匹配结束后开始匹配。您可以使用以下内容(Perl):

之后,您应该在$res上有结果字符串。运行此脚本时,我的输出是:

public static PropertyKey Audio\u ChannelCount=new PropertyKey(新Guid({64440490-4C8B-11D1-8B70-080036B11A03}))

免责声明:我不是Perl程序员,因此如果此代码中存在任何重大错误,请随时更正

// 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\);
(0x([[:xdigit:]]){2},\s*)+
my $text = "DEFINE_PROPERTYKEY(PKEY_Audio_ChannelCount, 0x64440490, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03, 7);";
my $res = "public static PropertyKey Audio_ChannelCount = new PropertyKey(new Guid(\"{";

if($text =~ m/0x((?:\d|[A-F]){8}),\s*0x((?:\d|[A-F]){4}),\s*0x((?:\d|[A-F]){4})/gc)
{
   $res .= $1 . "-" . $2 . "-" . $3 . "-";
}

if($text =~ m/\G,\s*0x((?:\d|[A-F]){2}),\s*0x((?:\d|[A-F]){2})/gc)#
{
   $res .= $1 . $2 . "-";
}

while($text =~ m/\G,\s*0x((?:\d|[A-F]){2})/gc)
{
   $res .= $1;
}

$res .= "}\"))";

print $res . "\n";