1

I want to extract a substring from a string, which is conform to a certain regex. The regex is:

(\[\s*(\d)+ byte(s)?\s*\](\s*|\d|[A-F]|[a-f])+)

Which effectively means that all of these strings get accepted:

[4 bytes] 66 74 79 70 33 67 70 35
[ 4 bytes ] 66 74 79 70 33 67 70 35
[1 byte] 66 74 79 70 33 67 70 35

I want to extract only the amount of bytes (just the number) from this string. I thought of doing this with re.search, but I'm not sure if that will work. What would be the cleanest and most performant way of doing this?

0

1 Answer 1

6

Use match.group to get the groups your regular expression defines:

import re

s = """[4 bytes] 66 74 79 70 33 67 70 35
[ 4 bytes ] 66 74 79 70 33 67 70 35
[1 byte] 66 74 79 70 33 67 70 35"""
r = re.compile(r"(\[\s*(\d)+ byte(s)?\s*\](\s*|\d|[A-F]|[a-f])+)")

for line in s.split("\n"):
    m = r.match(line)
    if m:
        print(m.group(2))

The first group matches [4 bytes], the second only 4.

Output:

4
4
1
Sign up to request clarification or add additional context in comments.

4 Comments

OP only wants the number of bytes!
@pidgey (\s*|\d|[A-F]|[a-f])+ should be rewritten as [\s\dA-Fa-f]+ seeing as you don't care about what it captures.
@nhahtdh Please comment the question about the regex.
@Tichodroma: I leave it here so that you can consider to include it in your answer. You can leave it alone if you don't want to. I have already pinged the OP in my comment.

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.