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: 
You're using a partial pattern, so you can't use `re.match`, which expects to match the entire input string. You need to use `re.search`, which can perform partial matches.

```
>>> that = "[0x2b]"
>>> m = re.search("0x([0-9,a-f]{2})", that)
>>> m.group()
'0x2b'

```
You want to use `re.search`. [This explains why.](http://docs.python.org/library/re.html#matching-vs-searching)