2016-09-28 30 views
3

我在學習Python,並希望在網絡安全類中自動執行我的任務之一。 我想弄清楚如何查找被一組括號綁定的文件的內容。該文件(.txt)的內容是這樣的:如何在python中查找括號綁定的字符串

cow.jpg : jphide[v5](asdfl;kj88876) 
fish.jpg : jphide[v5](65498ghjk;0-) 
snake.jpg : jphide[v5](poi098*/[email protected]#) 
test_practice_0707.jpg : jphide[v5](sJ*[email protected]&Ve!2) 
test_practice_0101.jpg : jphide[v5](nKFdFX+C!:V9) 
test_practice_0808.jpg : jphide[v5](!~rFX3FXszx6) 
test_practice_0202.jpg : jphide[v5](X&aC$|mg!wC2) 
test_practice_0505.jpg : jphide[v5](pe8f%yC$V6Z3) 
dog.jpg : negative` 

這裏是我到目前爲止的代碼:

import sys, os, subprocess, glob, shutil 

# Finding the .jpg files that will be copied. 
sourcepath = os.getcwd() + '\\imgs\\' 
destpath = 'stegdetect' 
rawjpg = glob.glob(sourcepath + '*.jpg') 

# Copying the said .jpg files into the destpath variable 
for filename in rawjpg: 
    shutil.copy(filename, destpath) 

# Asks user for what password file they want to use. 
passwords = raw_input("Enter your password file with the .txt extension:") 
shutil.copy(passwords, 'stegdetect') 

# Navigating to stegdetect. Feel like this could be abstracted. 
os.chdir('stegdetect') 

# Preparing the arguments then using subprocess to run 
args = "stegbreak.exe -r rules.ini -f " + passwords + " -t p *.jpg" 

# Uses open to open the output file, and then write the results to the file. 
with open('cracks.txt', 'w') as f: # opens cracks.txt and prepares to w 
     subprocess.call(args, stdout=f) 

# Processing whats in the new file. 
f = open('cracks.txt') 
+0

什麼樣的paranthesis的?你可以使用正則表達式,但你需要告訴實際需求能夠幫助你。 – prabodhprakash

+0

如果破解的密碼包含括號會怎麼樣? – vz0

回答

2

如果它應該只是受其約束,(和)你可以使用以下正則表達式,它確保啓動(和關閉),並且你可以在它們之間有數字和字符。您也可以添加任何其他符號,您想要包括。

[\(][a-z A-Z 0-9]*[\)]

[\(] - starts the bracket 
[a-z A-Z 0-9]* - all text inside bracket 
[\)] - closes the bracket 

所以對於輸入sdfsdfdsf(sdfdsfsdf)sdfsdfsdf,輸出將是(sdfdsfsdf) 測試這個表達式在這裏:https://regex101.com/

0

我正在學習Python的

如果你正在學習你應該考慮替代實現,而不僅僅是regexp秒。

通過一個文本文件,你只需打開該文件的線和在文件句柄來遍歷行:

with open('file.txt') as f: 
    for line in f: 
     do_something(line) 

每一行與行內容的字符串,包括最終的行的字符'/ N'。要查找您可以使用一個字符串特定字符串的開始索引發現:

>>> A = "hello (world)" 
>>> A.find('(') 
6 
>>> A.find(')') 
12 

要獲得從字符串的子串,你可以使用切片符號形式:

>>> A[6:12] 
'(world' 
+0

對不起,這可能是一個愚蠢的問題,但是通過考慮替代實現,你是什麼意思?像IronPython一樣?你有什麼建議嗎? –

+0

@ParkerPierce對於每個編程問題,都有很多解決方案和實現。您可以使用正則表達式解決您的問題,但您也可以通過編寫代碼和進行字符串處理來解決問題。由於您剛剛開始,因此探索不同類型的解決方案是符合您的最佳利益的。 – vz0

0

你應該使用的是在Python re module

實現正則表達式像\(.*\)一個簡單的正則表達式可以匹配你的「括號字符串」 但它是一組\((.*)\),讓只得到更好的第括號中的內容。

import re 

test_string = """cow.jpg : jphide[v5](asdfl;kj88876) 
fish.jpg : jphide[v5](65498ghjk;0-) 
snake.jpg : jphide[v5](poi098*/[email protected]#) 
test_practice_0707.jpg : jphide[v5](sJ*[email protected]&Ve!2) 
test_practice_0101.jpg : jphide[v5](nKFdFX+C!:V9) 
test_practice_0808.jpg : jphide[v5](!~rFX3FXszx6) 
test_practice_0202.jpg : jphide[v5](X&aC$|mg!wC2) 
test_practice_0505.jpg : jphide[v5](pe8f%yC$V6Z3) 
dog.jpg : negative`""" 

REGEX = re.compile(r'\((.*)\)', re.MULTILINE) 

print(REGEX.findall(test_string)) 
# ['asdfl;kj88876', '65498ghjk;0-', 'poi098*/[email protected]#', 'sJ*[email protected]&Ve!2', 'nKFdFX+C!:V9' , '!~rFX3FXszx6', 'X&aC$|mg!wC2', 'pe8f%yC$V6Z3'] 
相關問題