0

我目前正在解析 .cpp 文件中的字符串,并且需要一种使用 _T 语法显示多行字符串块的方法。为了排除一行 _T 字符串,我包含了一个 -notmatch ";" 参数来排除它们。这也排除了我需要的字符串块的最后一行。所以我需要显示下一个字符串,让最后一个字符串块用“;” 已经包括了。

我试过 $foreach.moveNext() | out-file C:/T_Strings.txt -append 但没有运气。

任何帮助将不胜感激。:)

    foreach ($line in $allLines)

    {

    $lineNumber++

    if ($line -match "^([0-9\s\._\)\(]+$_=<>%#);" -or $line -like "*#*" -or $line -like "*\\*" -or $line -like "*//*" -or $line -like "*.dll* *.exe*")
    {
        continue
    } 

    if ($line -notlike "*;*" -and $line -match "_T\(\""" ) # Multiple line strings
    {
        $line | out-file C:/T_Strings.txt -append
        $foreach.moveNext() | out-file C:/T_Strings.txt -append
    }
4

2 回答 2

1

在您的示例中,$foreachis 不是变量,因此您不能在其上调用方法。如果你想要一个迭代器,你需要创建一个:

$iter = $allLines.GetEnumerator()

do
{
    $iter.MoveNext()
    $line = $iter.Current
    if( -not $line )
    {
        break
    }
} while( $line )

不过,我建议您不要使用正则表达式。改为解析 C++ 文件。这是我能想到的最简单的解析所有_T 字符串的方法。它不处理:

  • 注释掉 _T 字符串
  • _T 字符串中的 ")
  • 文件末尾的 _T 字符串。

您必须自己添加这些检查。如果你只想要多行 _T 字符串,你也必须过滤掉单行字符串。

$inString = $false
$strings = @()
$currentString = $null

$file = $allLines -join "`n"
$chars = $file.ToCharArray()
for( $idx = 0; $idx < $chars.Length; ++$idx )
{
    $currChar = $chars[$idx]
    $nextChar = $chars[$idx + 1]
    $thirdChar = $chars[$idx + 2]
    $fourthChar = $chars[$idx + 3]

    # See if the current character is the start of a new _T token
    if( -not $inString -and $currChar -eq '_' -and $nextChar -eq 'T' -and $thirdChar -eq '(' -and $fourthChar -eq '"' )
    {
        $idx += 3
        $inString = $true
        continue
    }

    if( $inString )
    {
        if( $currChar -eq '"' -and $nextChar -eq ')' )
        {
            $inString = $false
            if( $currentString )
            {
                $strings += $currentString
            }
            $currentString = $null
        }
        else
        {
            $currentString += $currChar
        }
    }
}
于 2012-06-06T21:12:23.577 回答
1

想出执行此操作的语法:

$foreach.movenext()
$foreach.current | out-file C:/T_Strings.txt -append

您需要移动到下一个,然后通过管道传输当前的 foreach 值。

于 2012-06-07T21:20:36.140 回答