Python bug, or am I misunderstanding something?

  • Context: Python 
  • Thread starter Thread starter Borek
  • Start date Start date
  • Tags Tags
    Bug Python
Click For Summary
SUMMARY

The forum discussion centers on a Python issue related to string formatting and regular expressions. The user encounters unexpected output when using the `m.group()` method from the `re` module, specifically with the pattern `re.compile('\[(\d+)\]\[(\d+)\](.*)')`. The output appears garbled when using `m.group()` strings, while direct string formatting works correctly. The user concludes that using `m.group(1)`, `m.group(2)`, and `m.group(3)` resolves the issue, indicating a misunderstanding of how to access matched groups.

PREREQUISITES
  • Understanding of Python string formatting
  • Familiarity with Python's `re` module for regular expressions
  • Basic knowledge of Python version differences, particularly in string handling
  • Experience with Raspbian operating system and package management
NEXT STEPS
  • Research Python string formatting techniques, specifically using `str.format()`
  • Learn about Python regular expressions and the `re` module in detail
  • Explore methods for upgrading Python on Raspbian, including troubleshooting missing files
  • Investigate common issues with string handling in older Python versions
USEFUL FOR

Beginner Python programmers, hobbyists experimenting with regular expressions, and users troubleshooting Python issues on Raspbian.

Borek
Mentor
Messages
29,203
Reaction score
4,625
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   Reactions: 1 person
Thanks. Much better now.

Will see if that's the end of the story :wink:
 

Similar threads

Replies
55
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K