Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python bug, or am I misunderstanding something?

  1. Oct 16, 2013 #1

    Borek

    User Avatar

    Staff: Mentor

    I don't know python, yet I am trying to code something, just for fun. But I have hit some strange obstacle and I don't know if it is a bug in my Python version (dated, it is over a year old, under Raspbian), or is it me misunderstanding something?

    Code (Text):
    pattern = re.compile('\[(\d+)\]\[(\d+)\](.*)')
    line = '[123][456]789'
    print 'original string:'
    print line
    m = pattern.match(line)
    print 'matched substrings:'
    print m.groups()
    fst = '[{}][{}]{}'
    print 'output using strings returned from m.group():'
    print fst.format(m.group(0), m.group(1), m.group(2))
    print 'output using just strings:'
    print fst.format('123','456','789')
    Output:

    Code (Text):
    original string:
    [123][456]789
    matched substrings:
    ('123', '456', '789')
    output using strings returned from m.group():
    [[123][456]789][123]456
    output using just strings:
    [123][456]789
    Why is the output using m.group() strings garbled? Strings are matched correctly, m.group() returns a string, so it should work the same way it works in the last output line - but it doesn't. I understand [] can be used to access arguments’ items, but I don't see a way of escaping them, besides, it doesn't seem to be a problem in other cases.

    The simplest thing to try would be to upgrade to the newest python version, but I am unable to. I get message that some files are missing and suggestion to do full update - which I don't want to try, as I was not able to setup my WiFi dongle on another Pi with the newest Raspbian.
     
  2. jcsd
  3. Oct 16, 2013 #2

    jhae2.718

    User Avatar
    Gold Member

    Try:
    Code (Text):

    print fst.format(m.group(1), m.group(2), m.group(3))
     
    m.group(0) returns the entire string '[123][456]789'.
     
  4. Oct 16, 2013 #3

    Borek

    User Avatar

    Staff: Mentor

    Thanks. Much better now.

    Will see if that's the end of the story :wink:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python bug, or am I misunderstanding something?
  1. Am I doing it right? (Replies: 2)

Loading...