使用 RegEx 从 .txt 文件中提取变量名称

使用 RegEx 从 .txt 文件中提取变量名称

我想$00incomplete_frame从文本文件中提取变量名(例如)并将名称放在变量中以供在 PowerShell 中使用。源变量名不包含空格。源文件行将多次包含变量名,但我只需要变量名一次。我试过:

$variable_names = @()
$samples = Get-Content C:<path to file>\code_clip.txt
foreach ($line in $samples) {$variable_names += (-$(\w+|\d+)){1} }
$variable_names.length
$variable_names

RegEx 有什么问题?

谢谢

编辑:这是示例文件的内容:

if ($00_motion_type -ne 0) {Write-Output "00 motion_type: $00_motion_type"}
if ($ac_tex -ne 0) {Write-Output "ac-tex damaged: $ac_tex"}
if ($delta_bit -ne 0) {Write-Output "delta bit allocation strategy reserved: $delta_bit"}
if ($end_mismatch -ne 0) {Write-Output "end mismatch: $end_mismatch"}
if ($audio_block -ne 0) {Write-Output "error decoding the audio block: $audio_block"}
if ($decoding_stream -ne 0) {Write-Output "Error while decoding stream: $decoding_stream"}
if ($expacc -ne 0) {Write-Output "expacc: $expacc"}
if ($exponent -ne 0) {Write-Output "exponent: $exponent"}
if ($f_mb_incr -ne 0) {Write-Output "first mb_incr damaged: $f_mb_incr"}
if ($CRC_mismatch -ne 0) {Write-Output "frame CRC mismatch: $CRC_mismatch"}
if ($pic_cod_ext -ne 0) {Write-Output "ignoring pic cod ext: $pic_cod_ext"}
if ($seq_ext -ne 0) {Write-Output "ignoring seq_ext: $seq_ext"}
if ($GOP_START_CODE -ne 0) {Write-Output "ignoring GOP_START_CODE: $GOP_START_CODE"}
if ($incomplete_frame -ne 0) {Write-Output "incomplete frame: $incomplete_frame"}
if ($increasing_dts -ne 0) {Write-Output "increasing dts: $increasing_dts"}
if ($invalid_cbp -ne 0) {Write-Output "invalid cbp: $invalid_cbp"}
if ($coupling_range -ne 0) {Write-Output "invalid coupling range: $coupling_range"}
if ($frame_dimensions -ne 0) {Write-Output "Invalid frame dimensions: $frame_dimensions"}
if ($mb_type -ne 0) {Write-Output "invalid mb type: $mb_type"}
if ($matrix_damaged -ne 0) {Write-Output "matrix damaged: $matrix_damaged"}
if ($mb_incr -ne 0) {Write-Output "mb incr damaged: $mb_incr"}
if ($picture_start_code -ne 0) {Write-Output "Missing picture start code: $picture_start_code"}
if ($new_coupling_coordinates -ne 0) {Write-Output "new coupling coordinates: $new_coupling_coordinates"}
if ($new_coupling_strategy -ne 0) {Write-Output "new coupling strategy: $new_coupling_strategy"}
if ($overread -ne 0) {Write-Output "overread: $overread"}
if ($qscale -ne 0) {Write-Output "qscale == 0: $qscale"}
if ($sequence_header -ne 0) {Write-Output "sequence header damaged: $sequence_header"}
if ($previntra -ne 0) {Write-Output "skip with previntra: $previntra"}
if ($skipped_MB -ne 0) {Write-Output "skipped MB: $skipped_MB"}
if ($slice_below_image -ne 0) {Write-Output "slice below image: $slice_below_image"}
if ($slice_mismatch -ne 0) {Write-Output "slice mismatch: $slice_mismatch"}
if ($MVs_not_available -ne 0) {Write-Output "Warning MVs not available: $MVs_not_available"}
if ($othererror -ne 0) {Write-Output "Other error(s): $othererror"}

我用它来选择已知输入正确的变量名,并且我想在每个变量名后附加“=0”,以便将整个列表复制并粘贴到代码中,这样它就可以用于将变量重置为 0。

答案1

这产生了我想要的结果:

$variable_names = @()
$reset_variables = @()
$samples = Get-Content C:<path to file>\code_clip.txt
foreach ($line in $samples) {$test= $line -match '\$\w+'; $variable_names += $matches.values }
foreach ($line in $variable_names) {$reset_variables += $line + "=0;" }
$reset_variables.length
$reset_variables

结果:

33
$00_motion_type=0;
$ac_tex=0;
$delta_bit=0;
$end_mismatch=0;
$audio_block=0;
$decoding_stream=0;
$expacc=0;
$exponent=0;
$f_mb_incr=0;
$CRC_mismatch=0;
$pic_cod_ext=0;
$seq_ext=0;
$GOP_START_CODE=0;
$incomplete_frame=0;
$increasing_dts=0;
$invalid_cbp=0;
$coupling_range=0;
$frame_dimensions=0;
$mb_type=0;
$matrix_damaged=0;
$mb_incr=0;
$picture_start_code=0;
$new_coupling_coordinates=0;
$new_coupling_strategy=0;
$overread=0;
$qscale=0;
$sequence_header=0;
$previntra=0;
$skipped_MB=0;
$slice_below_image=0;
$slice_mismatch=0;
$MVs_not_available=0;
$othererror=0;

由于已知变量名是正确的并且可以正常工作,我可以使用它来复制并粘贴到代码中以将它们清零。

实际上,我使用下面的内容进行水平输出:

$variable_names = @()
$reset_variables = ""
$samples = Get-Content C:<path to file>\code_clip.txt
foreach ($line in $samples) {$test= $line -match '\$\w+'; $variable_names += $matches.values }
foreach ($line in $variable_names) {$reset_variables = $reset_variables + $line + '=0; ' }
$reset_variables
$00_motion_type=0; $ac_tex=0; $delta_bit=0; $end_mismatch=0; $audio_block=0; $decoding_stream=0; $expacc=0; $exponent=0; $f_mb_incr=0; $CRC_mismatch=0; $pic_cod_ext=0; $seq_ext=0; $GOP_START_CODE=0; $incomplete_frame=0; $increasing_dts=0; $invalid_cbp=0; $coupling_range=0; $frame_dimensions=0; $mb_type=0; $matrix_damaged=0; $mb_incr=0; $picture_start_code=0; $new_coupling_coordinates=0; $new_coupling_strategy=0; $overread=0; $qscale=0; $sequence_header=0; $previntra=0; $skipped_MB=0; $slice_below_image=0; $slice_mismatch=0; $MVs_not_available=0; $othererror=0;

相关内容