本帖最后由 WHY 于 2019-5-25 12:32 编辑
- $global:n = 0; $Hash = @{};
- $str = [IO.File]::ReadAllText('a.txt',[Text.Encoding]::Default);
-
- #修改name属性的值,如果name属性值以music_prev或music_display或music_next等开头,或者节点名为Extra,则不修改
- #如果command与target同时出现,command="play",则修改target属性值,
- $reg1 = '(?<=<(?!Extra\s)[^<>]*\sname=")(?!music_(?:prev|display|next|play|pause))[^"]+(?=")';
- $reg2 = '(?<=\starget=")[^"]+(?="[^<>]*\scommand="play")|(?<=\scommand="play"[^<>]*\starget=")[^"]+(?=")';
-
- $str = [regex]::Replace($str, $reg1 + '|' + $reg2, {
- param($m);
- $key = $m.Value;
- if( !$Hash.ContainsKey($key) ){
- $x = [Math]::floor( $global:n / 26 );
- if($x) { $chr1 = [char]( 64 + [int]$x ) } else { $chr1 = '' }
- $Hash[$key] = $chr1 + [char]( 65 + $global:n++ % 26 );
- }
- $Hash[$key];
- })
-
- $arrKey = $Hash.Keys | sort -Desc {$_.Length};
-
- #如果属性值包含@#字符,或者属性值以 .animation或.visibility 结尾,则修改
- $str = [regex]::Replace($str, '(?<==")([^"@#]*[@#][^"]+|[^"]+(?:\.animation|\.visibility))(?=")', {
- param($m);
- $s = $m.Groups[1].Value;
- forEach( $key In $arrKey ) {
- $s = $s.Replace( '@' + $Key, '@' + $Hash[$Key] );
- $s = $s.Replace( '#' + $Key, '#' + $Hash[$Key] );
- $s = $s.Replace( $Key + '.animation', $Hash[$Key] + '.animation' );
- $s = $s.Replace( $Key + '.visibility', $Hash[$Key] + '.visibility' );
- }
- $s;
- })
-
- [IO.File]::WriteAllText('b.txt', $str, [Text.Encoding]::UTF8);
-
- echo 'Done';
- [Console]::ReadKey()
复制代码
|