I wrote this regex in an online editor and it was working properly there, but whenever I copy it to python, it can't find what I need anymore. Here is my code:
import requests
import re
import os
PAYLOAD = {
"username": os.environ['USERNAME'],
"password": os.environ['PASSWORD']
}
LOGIN_URL = "https://ringzer0ctf.com/login"
CHALLENGE_URL = "https://ringzer0ctf.com/challenges/32"
MESSAGE_REG_EX = r"(BEGIN MESSAGE -----<br \/>\n\n\t\t)(\d*)"
with requests.session() as session:
session.post(LOGIN_URL, data=PAYLOAD)
r = session.get(CHALLENGE_URL)
print(r.text)
num = re.search(MESSAGE_REG_EX, r.text).group(2)
print(num)
I need to extract the first number between BEGIN MESSAGE and END MESSAGE which in this case is 6907.
<div class="challenge-wrapper">
<div class="padding_div">
</div>
<strong>You have 2 seconds to send the answer</strong><br />
<strong>Send the answer back using https://ringzer0ctf.com/challenges/32/[answer]</strong>
<br /><br /><br /><br /><br />
<div class="message">
----- BEGIN MESSAGE -----<br />
6907 + 0x1d68 - 1010001001100 = ?<br />
----- END MESSAGE -----<br />
</div>
</div>
<hr />
Error message:
num = re.search(MESSAGE_REG_EX, r.text).group(2) AttributeError: 'NoneType' object has no attribute 'group'
MESSAGE_REG_EX = r"(?s)(BEGIN MESSAGE -{3,}<br\s*\/>\s*)(\d+)"r"(BEGIN MESSAGE -----<br />\n\s*)(\d+)"should work. Note there is no point escaping/, it is not any special metacharacter.