2012-04-22 108 views
0

我需要提取C源文件中定義的函數名作爲參數給腳本。 我不PowerShell的很熟悉,但不應該是這樣的:powershell正則表達式 - 提取函數名稱C源程序

If ($FileExists -eq $True) 
{ 
    select-string $args[0] -pattern "\(void\|double\|char\|int\) \.*(.*)" 
} 
+0

我不應該逃脫字符所謂>'選擇字符串的$ args [0] -pattern「(無效|雙| char * int)\。*(。*)「'但我該如何引用函數的名字(類似linux-sed會很有用) – NiCU 2012-04-22 20:59:56

回答

2

我會定義要在搜索和中包含的參數使用這些文件的擴展名,然後通過管道的列表選擇字符串並提取捕獲組匹配。

dir -Path C:\c-program -Include *.h, *.c -Recurse | Select-String -Pattern '(void|double|char|int) (\w+)\(' | % {$_.Matches[0].Groups[2].Value} 

下面是使用函數名來自維基百科罐頭例如:

'void incInt(int *y)','int main(int argc, char* args[])','int subtract(int x, int y)' | Select-String -Pattern '(void|double|char|int) (\w+)\(' | % {$_.Matches[0].Groups[2].Value}