Search code examples
pythonregexhex

Regular expression for hexadecimal string in python not working


I have a regular expression to match strings like:

--D2CBA65440D

--77094A27E09

--77094A27E

--770

--77094A27E09--

basically, it matches a hexadecimal string surrounded by one or more line breaks or white space, and has the prefix -- and may or may not have -- as suffix

i use the following python code, and it works fine most of the time:

hexaPattern = "\s--[0-9a-fA-F]+[--]?\s"
hex = re.search(hexaPattern, part)
if hex:
   print "found a match"

this works for all of the above but it doesn't match --77094A27E09 in this block:

<div id="arrow2" class="headerLinksImg" style="display:block

--77094A27E09

;">

but matches the same string in:

<input type="checkbox" name="checkbox" id="checkboxKG3" class

--77094A27E09

Content-T="checkboxKG" value="KG3" />

What am i doing wrong?


Solution

  • I used the following :

    pattern = re.compile(r'(\n--)([0-9A-F]+)(--)?', re.I | re.S | re.M)
    

    and it worked fine. Thanks to all your contributions.