0

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'

2
  • 1
    try MESSAGE_REG_EX = r"(?s)(BEGIN MESSAGE -{3,}<br\s*\/>\s*)(\d+)" Commented Aug 13, 2019 at 11:18
  • 1
    I think the problem is with whitespace: r"(BEGIN MESSAGE -----<br />\n\s*)(\d+)" should work. Note there is no point escaping /, it is not any special metacharacter. Commented Aug 13, 2019 at 11:19

1 Answer 1

1

Maybe - or spaces got outnumbered. Give a try to below regex,

MESSAGE_REG_EX = r"BEGIN MESSAGE -{3,}<br\s*\/>\s*(\d+)" 
match = re.search(MESSAGE_REG_EX, r.text)
if match:
    print (match.group(1))
else:
    print('Failed to find a match :(')
Sign up to request clarification or add additional context in comments.

1 Comment

(?s) is redundant here. \/ = /. Basically, I'd suggest using a single capturing group and access its value with .group(1), OP is not interested in the BEGIN MESSAGE... part. And it is a good idea to show OP how to write code to avoid AttributeError: 'NoneType' object has no attribute 'group' exceptions upon no match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.