本帖最后由 WHY 于 2022-1-13 18:03 编辑
- $arr = gc a.txt -ReadCount 0;
- $count = $arr.Count;
- $out = [Collections.ArrayList]@();
-
- for ( $i = 0; $i -lt $count; $i++ ){
- $m = [regex]::match($arr[$i], '^\d+\..*\(\s*([A-Z])\s*\)$'); #匹配开头为数字+".",结尾为"([A-Z])"的行
- if ( $m.Success ) {
- $Queston = $arr[$i]; #整行赋值给变量 Queston
- $Alph = $m.Groups[1].Value; #字母A-Z赋值给 Alph
- } elseIf ( Test-Path Variable:Alph ) { #如果变量 Alph 已定义
- $m = $arr[$i] -match $Alph + '\.(?:(?![A-Z]\.).)*'; #匹配答案
- if ( $m ) {
- $Answer = $matches[0]; #答案赋值给 Answer
- [void]$out.Add( $Queston + "`r`n" + $Answer ); #问题和答案加入数组 out
- }
- }
- }
-
- sc b.txt -Value $out; #输出
复制代码
|