我有一個文件格式如下:Python的正則表達式匹配,直到字符多行
hello = {
a = "2354a"
b = "06567567h"
}
goodbye = {
there = "/home/afhge"
}
...
anotherset = {
dsfsdf = grhbrwecs
dfgtmyj = 12345
}
我正在使用python的正則表達式,我想匹配的是括號內的一切因此產生的匹配輸出會以下列表:
['\n\n\ta = "2345a"\n\tb = "06567567h"\n\n\n', '\n\there = "/home/afhge"\n\n', '\n\tdsfsdf = grhbrwecs\n\tdfgtmyj = 12345\n\n']
我已經試過正則表達式:
desired_output = re.findall("{[^}]", file_text)
howeve這個正則表達式的結果在列表中:
['{\n', '{\n', '{\n', '{\n', '{\n']
它看起來像[^}]匹配任何字符,直到換行。我試着這樣做:!
desired_output = re.findall("{[^}]", file_text, re.S)
和
desired_output = re.findall("{[^}]", file_text, re.M)
要沒有成功:(
感謝
驚人的這個工作的感謝! 至於不包括{在比賽中,我只是圍繞我想顯示的代碼括號。我的新正則表達式: 「\ {([^ \}] *)」 – m3hughes
@ m3hughes yup這是一種不同的可能性。 –