To match text within square brackets that cannot have [
and ]
inside it, but should contain some other text can be matched with a [^][]
negated character class.
That is, you may match the whole text within square brackets with [[^][]*]
, and if you need to match some text inside, you need to put that text after [^][]*
and then append another occurrence of [^][]*
before the closing ]
.
You may use
re.findall(r'[([^][]*"key"[^][]*)]', text_file.read())
See the Python demo:
import re
s = '''INFO:werkzeug:127.0.0.1 - - [20/Sep/2018 19:40:00] "GET /socket.io/?polling HTTP/1.1" 200 -
INFO:engineio: Received packet MESSAGE, ["key",{"data":{"tag1":12,"tag2":13,"tag3": 14"...}}]'''
print(re.findall(r'[([^][]*"key"[^][]*)]', s))
Output:
['"key",{"data":{"tag1":12,"tag2":13,"tag3": 14"...}}']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…