0

I have to extract a string from a network response which looks like shown below:

Hello, did you get this message?
I want to check it,
let me know!

Here is your encrypted text:
96a1e3424f4cfa23db131173d7f8c93396a1e3424f4cfa23db131173d7f8c933182bc4e99a47abb5deaa51741527dd2b478746563aecc40d5d6f6597370338a7

Some more text:

The response contains "\r\n" or if checked on Linux, then "\n"

How can I extract the hash from above message?

I wrote something like below but it did not extract the hash:

import re

# data corresponds to network response which contains the \r\n characters

matchObj = re.match( r'(.*?)your encrypted text:\r\n(.*)\r\n.*', data)

if matchObj:
    print matchObj.group(2)

Thanks.

3
  • 1
    Your regex does not fit your text at all. Commented Nov 25, 2018 at 4:49
  • Where is your encrypted text: in your input? Commented Nov 25, 2018 at 5:01
  • I updated it now. That string is in the input itself. Commented Nov 27, 2018 at 2:40

2 Answers 2

1

Read the input one line at a time (using readlines() or split('\n')) and do this:

for line in lines:
    match = re.match('^([0-9a-f]+)$', line)
    if match:
        print(match.groups(1)[0])
Sign up to request clarification or add additional context in comments.

Comments

1

If it's okay that you don't use regex, try it:

lines = open(file_name, 'r').read().splitlines()

for i, line in enumerate(lines):
    if line.strip() == "your encrypted text:":
        my_text = lines[i + 1]
        break

Comments

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.