2013-08-19 25 views
0

我正在使用power-shell並從我的程序中獲取下面的輸出。正則表達式從一長串亂碼中獲取密碼

我有問題從其他事情混亂中獲取密碼。理想情況下,我需要自己獲得Hiva !! 66。我正在使用reg-ex來完成這個任務,而它只是不工作。密碼將始終爲8個字符,分別具有大寫和小寫以及特殊字符。我已經創建了拆分以及我需要的所有其他東西,但是reg-ex部分與我混淆了。

我不在乎有很多關於reg-ex和密碼的問題,但是在它之前和之後似乎沒有太多的混亂。任何幫助將不勝感激。 我最好的嘗試,到目前爲止是:

"(?=.*\d)(?=.*[A-Z])(?=.*[[email protected]#\$%\^&\*\~()_\+\-={}\[\]\\:;`"'<>,./]).{8}$" 

C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\CONNECTEXP.VCB:5:For intTmp = 1 To 4 
C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\CONNECTEXP.VCB:8:cboCOMPort.SelectString 1, "1" 
C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\CONNECTEXP.VCB:11:str2CRLF = Chr(13) & Chr(10) & Chr(13) & Chr(10) 
C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\CONNECTEXP.VCB:14: & "include emulation type (currently Tandem), the I/O method (currently Async) and host connection information 
for the session (currently COM9, 8N1)" _ 
C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\CONNECTEXP.VCB:15: & " to the correct values for your target host (e.g., TCP/IP and host IP name or address) and save the 
IOSet "CHARSIZE", "8" 
PASS="Hiva!!66" If DDEAppReturnCode() <> 0 Then 
If DDEAppReturnCode() <> 0 Then 
C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\DDEtoXL.vcb:28: MsgBox "Could not load " & txtWorkSheet.text, 48 
C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\DDEtoXL.vcb:37:DDESheetChan = -1 
C:\Users\<username>\AppData\Roaming\Crystal Point\OutsideView\Macro\DDEtoXL.vcb:38:DDESystemChan = -2 

回答

0

如果你不能在報價或PASS=在那裏算,你就必須依靠密碼的組成做的一切。以下正則表達式匹配允許類型的八個連續字符的字符串,前向和後向確保不超過八個字符。

$regex = [regex] @' 
(?x) 
(?<![[email protected]#$%^&*~()_+\-={}\[\]\\:;`<>,./A-Za-z0-9]) 
(?: 
    [[email protected]#$%^&*~()_+\-={}\[\]\\:;`<>,./]() 
    | 
    [A-Z]() 
    | 
    [a-z]() 
    | 
    [0-9]() 
){8} 
\1\2\3\4 
(?![[email protected]#$%^&*~()_+\-={}\[\]\\:;`<>,./A-Za-z0-9]) 
'@ 

它還驗證至少有一種字符類型:大寫字母,小寫字母,數字和特殊字符。您的正則表達式中使用的先行式方法將無法正常工作,因爲它可能會超出您嘗試匹配的單詞的末尾。相反,我在每個分支中放置一個空組以充當複選框。如果對其中一個組的反向引用失敗,則意味着分支沒有參與匹配,這意味着相關的字符類型不存在。

+0

歡呼聲,這對我很好:) – user1345603

1

你試試下面的正則表達式:

^PASS="(.{8})" 

+0

是的,我做了,但有時它不會讓這個詞在前面傳遞。任何具有要求的8位數字字符串是我所追求的。上,下,特殊數字和8位數字。有時它不會有密碼附近的「」...... – user1345603

+0

這個文件很混亂。 ><密碼總是在同一行上?在行首?在一些特定/獨特的詞後? – fabien

0

你可以用這樣的提取,可輸出密碼:

... | ? { $_ -cmatch 'PASS="(.{8})"' | % { $matches[1] } 

或像這樣(在PowerShell中V3):

... | Select-String -Case 'PASS="(.{8})"' | % { $_.Matches.Groups[1].Value } 

在PowerShell v2中,如果您想使用Select-String,則必須執行此操作:

... | Select-String -Case 'PASS="(.{8})"' | select -Expand Matches | 
    select -Expand Groups | select -Last 1 | % { $_.Value }