Ram
Ram

Reputation: 557

Python re.match returns none. How to modify the search string?

Below are my lines:

num1    -num2 num3: var.shift varstate=shift, var3=num, var4=False/True,  Shift   0xabc5d 

num1    -num2 num3: var.shift varstate=shift, var3=num, var4=False/True,  Shift 0xabc08 

num1    -num2 num3: var.shift varstate=shift, var3=num, var4=False/True,  Shift [38b]0xabc34

I am trying to get the hexvalue from the lines. I need the hexvalue in only one of the line which is in the form of [38b]0xabc1234567890000000000000000000000000. I dont need all the hex values.

All my hex values int he line start with 0x so I have given that in re.match

#!/usr/bin/env python
import sys
import re
import binascii
import string
value_file = open("test2.txt","r")
file2 = open("out.txt", "w")

for line in value_file:
    line_string = str(line)
    line_nospace = line.replace(" ", "")
    lines_cont = line_nospace.rstrip("\n")
    reg_match = re.match(r'\[([38b]]*)\](0-9a-zA-Z)', lines_cont)
    print reg_match

Upvotes: 0

Views: 596

Answers (1)

James Henstridge
James Henstridge

Reputation: 43939

Your regular expression (.*) 0x(\.*) escapes the period in the second group, which means that it is matching zero or more periods rather than zero or more arbitrary characters.

If you want to match everything after the 0x, remove the back slash.

Upvotes: 2

Related Questions