Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 896751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:46:45+00:00 2026-05-15T14:46:45+00:00

Is python confused, or is the programmer? I’ve got a lot of lines of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:46:45+00:00Added an answer on May 15, 2026 at 2:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.