如何在Python中查找括号限定的字符串

3
我是一名学习Python的人,希望能够在我的网络安全课程中自动化其中一个作业。 我正在尝试找出如何查找由一组括号限定的文件内容。(.txt)文件的内容如下:
cow.jpg : jphide[v5](asdfl;kj88876)
fish.jpg : jphide[v5](65498ghjk;0-)
snake.jpg : jphide[v5](poi098*/8!@#)
test_practice_0707.jpg : jphide[v5](sJ*=tT@&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')

什么样的括号?您可以使用正则表达式,但需要告诉实际要求才能帮助您。 - prabodhprakash
如果破解的密码包含括号怎么办? - vz0
3个回答

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。
如果你正在学习,你应该考虑替代实现,不仅仅是正则表达式。
要逐行迭代文本文件,只需打开文件并在文件句柄上进行循环即可。
with open('file.txt') as f:
    for line in f:
        do_something(line)

每行都是一个字符串,包括换行符“/n”。要查找字符串中特定子串的起始索引,可以使用find方法:

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

要从字符串中获取子字符串,可以使用切片符号的形式:

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

对不起,这可能是一个愚蠢的问题,但是你说的“考虑其他实现”是什么意思?比如IronPython吗?你有什么建议吗? - Parker Pierce
@ParkerPierce 对于每个编程问题,都有许多解决方案和实现方法。你可以使用正则表达式来解决问题,但也可以通过编写代码和进行字符串处理来解决它。由于你刚开始学习,探索不同种类的解决方案对你最有利。 - vz0

0

您应该使用正则表达式,这是在 Python re模块中实现的。

\(.*\)这样的简单正则表达式可以匹配您的“括号字符串”,但最好使用\((.*)\)组,它允许仅获取括号中的内容。

import re

test_string = """cow.jpg : jphide[v5](asdfl;kj88876)
fish.jpg : jphide[v5](65498ghjk;0-)
snake.jpg : jphide[v5](poi098*/8!@#)
test_practice_0707.jpg : jphide[v5](sJ*=tT@&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*/8!@#', 'sJ*=tT@&Ve!2', 'nKFdFX+C!:V9' , '!~rFX3FXszx6', 'X&aC$|mg!wC2', 'pe8f%yC$V6Z3']

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接