Python regex confused by brackets ([])?
Question: Is python confused, or is the programmer?

I've got a lot of lines of this:

    
    
    some_dict[0x2a] = blah
    some_dict[0xab] = blah, blah
    

What I'd like to do is to convert the hex codes into all uppercase to look
like this:

    
    
    some_dict[0x2A] = blah
    some_dict[0xAB] = blah, blah
    

So I decided to call in the regular expressions. Normally, I'd just do this
using my editor's regexps (xemacs), but the need to convert to uppercase
pushes one into Lisp. ....ok... how about Python?

So I whip together a short script which doesn't work. I've condensed the code
into this example, which doesn't work either. It looks to me like Python's
regexps are getting confused by the brackets in the code. Is it me or Python?

    
    
    import fileinput
    import sys
    import re
    
    
    this = "0x2a"
    that = "[0x2b]"
    
    for line in [this, that]:
        found = re.match("0x([0-9,a-f]{2})", line)
    
        if found:
            print("Found: %s" % found.group(0))
    

(I'm using the () grouping constructs so I don't capitalize the 'x' in '0x'.)

This example only prints the 0x2a value, not the 0x2b. Is this correct
behavior?

I can easily work around this by changing the match expression to:

    
    
        found = re.match("\[0x([0-9,a-f]{2}\])", line)
    

but I'm just wondering if someone can give me some insight into what's going
on here.

Running Python 2.6.2 on Linux.


Answer: `re.match` matches from the _start_ of the string. Use `re.search` instead to
"match the first occurrence anywhere in the string". The key bit about this in
the docs is [here](https://docs.python.org/2/library/re.html#search-vs-match).

