Python Python bug, or am I misunderstanding something?

  • Thread starter Thread starter Borek
  • Start date Start date
  • Tags Tags
    Bug Python
AI Thread Summary
The discussion revolves around a coding issue encountered while using Python, specifically related to string formatting with regex matches. The user is trying to match a string with a regex pattern and format the output using the matched groups. The problem arises when using `m.group()` to format the output, resulting in unexpected garbled text. The user initially suspects a bug in their outdated Python version on Raspbian but later realizes that the issue is due to incorrect indexing when accessing the matched groups. By adjusting the format call to use the correct group indices, the output is resolved. The user acknowledges the need for a Python upgrade but faces challenges with their current setup, indicating a preference to avoid extensive updates due to prior difficulties with WiFi configuration on another device.
Borek
Mentor
Messages
29,132
Reaction score
4,556
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:
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:
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.
 
Technology news on Phys.org
Try:
Code:
print fst.format(m.group(1), m.group(2), m.group(3))

m.group(0) returns the entire string '[123][456]789'.
 
  • Like
Likes 1 person
Thanks. Much better now.

Will see if that's the end of the story :wink:
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top