本帖最后由 smss 于 2022-10-15 21:26 编辑
回复 2# WHY
老师
如果文本中含有GB就只把GB替换为G 而不添加G怎样改
这是要修改的文本->https://share.weiyun.com/AXZ1xaz5
249c GA104M [GeForce RTX 3080 Mobile / Max-Q 8GB/16GB]修改为249c GA104M [GeForce RTX 3080 Mobile / Max-Q 8G/16G]
我这照葫芦画瓢 知其然不知所以然的怕是要丢人了- param([string]$file); #修改前的文件
- $file2 = ".\1.txt"; #修改后的文件
- $Hash1 = @{ '5' = '4G'; '6' = '6G'; '7' = '8G'; '8' = '8G'}
- $Hash2 = @{ '5' = '4G'; '6' = '6G'; '7' = '8G'; '8' = '16G'; '9' = '24G' } #3050=4G 3060=6G 3070=8G 3080=10G
- $UTF8NoBom = New-Object System.Text.UTF8Encoding $false;
- $str = [IO.File]::ReadAllText($file, [Text.Encoding]::Default);
- $str = [regex]::Replace( $str, '\b10([5-8])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash1[$m.Groups[1].Value] } );
- $str = [regex]::Replace( $str, '\b20([5-8])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash1[$m.Groups[1].Value] } );
- $str = [regex]::Replace( $str, '\b30([5-9])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash2[$m.Groups[1].Value] } );
- $str = [regex]::Replace( $str, '\b40([5-9])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash2[$m.Groups[1].Value] } );
- $str = $str -replace '\r\n', "`n";
- [IO.File]::WriteAllText($file2, $str, $UTF8NoBom);
- echo 'Done';
- [Console]::ReadLine();
复制代码 148c 2357 Grenada XT [Radeon R9 390X] 也会被替换为148c 2357 Grenada XT [Radeon R9 390X 8G] |