notepad++ - Regex pattern repetition and capturing -
i had translate propkeys.h (in c[++]?) in c#.
my goal come from:
define_propertykey(pkey_audio_channelcount, 0x64440490, 0x4c8b, 0x11d1, 0x8b, 0x70, 0x08, 0x00, 0x36, 0xb1, 0x1a, 0x03, 7);
to:
public static propertykey audio_channelcount = new propertykey(new guid("{64440490-4c8b-11d1-8b70-080036b11a03}"));
i using notepad++ regex, i'm open other scriptable solution (perl, sed...). please no compiled language (as c#, java...).
i ended (working):
// turns guid string // find (line breaks inserted 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\);
while working, feel first pass odd. wanted replace tons of {2} repeating one. like:
(0x([[:xdigit:]]){2},\s*)+
but can't work groups. can tell me "standard" way of doing regexes ?
unfortunately when perform match using quantifier, group match whole text, more "classy" solution use equivalent perl's \g metacharacter, starts match after end of previous match. can use (perl):
my $text = "define_propertykey(pkey_audio_channelcount, 0x64440490, 0x4c8b, 0x11d1, 0x8b, 0x70, 0x08, 0x00, 0x36, 0xb1, 0x1a, 0x03, 7);"; $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";
after should have result string on $res. output when running script was:
public static propertykey audio_channelcount = new propertykey(new guid("{64440490-4c8b-11d1-8b70-080036b11a03}"))
disclaimer: i'm not perl programmer, if there substantial errors in code, please feel free correct them
Comments
Post a Comment